Skip to content

Commit 4802b27

Browse files
committed
style(my-samples): 格式化 Java 代码
- 使用 Intellij IDEA 的代码格式化功能格式化了所有 Java 文件 -调整了缩进、空格和换行 - 修复了一些小的语法问题
1 parent 6d9812d commit 4802b27

File tree

210 files changed

+5705
-5532
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

210 files changed

+5705
-5532
lines changed

.githooks/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
mvn spring-javaformat:validate

my-samples/auth-server-01-start/src/main/java/com/chensoul/AuthServerApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
@SpringBootApplication
77
public class AuthServerApplication {
8+
89
public static void main(String[] args) {
910
SpringApplication.run(AuthServerApplication.class, args);
1011
}
12+
1113
}

my-samples/auth-server-01-start/src/main/java/com/chensoul/config/SecurityConfig.java

Lines changed: 86 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -42,64 +42,54 @@
4242
@Configuration
4343
@EnableWebSecurity(debug = true)
4444
public class SecurityConfig {
45-
@Bean
46-
@Order(1)
47-
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http)
48-
throws Exception {
49-
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
50-
OAuth2AuthorizationServerConfigurer.authorizationServer();
51-
52-
http
53-
.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
54-
.with(authorizationServerConfigurer, (authorizationServer) ->
55-
authorizationServer
56-
.oidc(Customizer.withDefaults()) // Enable OpenID Connect 1.0
57-
)
58-
.authorizeHttpRequests((authorize) ->
59-
authorize
60-
.anyRequest().authenticated()
61-
)
62-
// Redirect to the login page when not authenticated from the
63-
// authorization endpoint
64-
.exceptionHandling((exceptions) -> exceptions
65-
.defaultAuthenticationEntryPointFor(
66-
new LoginUrlAuthenticationEntryPoint("/login"),
67-
new MediaTypeRequestMatcher(MediaType.TEXT_HTML)
68-
)
69-
);
70-
71-
return http.build();
72-
}
73-
74-
@Bean
75-
@Order(2)
76-
public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http)
77-
throws Exception {
78-
http
79-
.authorizeHttpRequests((authorize) -> authorize
80-
.anyRequest().authenticated()
81-
)
82-
// Form login handles the redirect to the login page from the
83-
// authorization server filter chain
84-
.formLogin(Customizer.withDefaults());
85-
86-
return http.build();
87-
}
88-
89-
@Bean
90-
public UserDetailsService userDetailsService() {
91-
UserDetails userDetails = User.withDefaultPasswordEncoder()
92-
.username("user")
93-
.password("password")
94-
.roles("USER")
95-
.build();
96-
97-
return new InMemoryUserDetailsManager(userDetails);
98-
}
9945

100-
@Bean
101-
public RegisteredClientRepository registeredClientRepository() {
102-
// @formatter:off
46+
@Bean
47+
@Order(1)
48+
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
49+
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer = OAuth2AuthorizationServerConfigurer
50+
.authorizationServer();
51+
52+
http.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
53+
.with(authorizationServerConfigurer,
54+
(authorizationServer) -> authorizationServer.oidc(Customizer.withDefaults()) // Enable
55+
// OpenID
56+
// Connect
57+
// 1.0
58+
)
59+
.authorizeHttpRequests((authorize) -> authorize.anyRequest().authenticated())
60+
// Redirect to the login page when not authenticated from the
61+
// authorization endpoint
62+
.exceptionHandling((exceptions) -> exceptions.defaultAuthenticationEntryPointFor(
63+
new LoginUrlAuthenticationEntryPoint("/login"), new MediaTypeRequestMatcher(MediaType.TEXT_HTML)));
64+
65+
return http.build();
66+
}
67+
68+
@Bean
69+
@Order(2)
70+
public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
71+
http.authorizeHttpRequests((authorize) -> authorize.anyRequest().authenticated())
72+
// Form login handles the redirect to the login page from the
73+
// authorization server filter chain
74+
.formLogin(Customizer.withDefaults());
75+
76+
return http.build();
77+
}
78+
79+
@Bean
80+
public UserDetailsService userDetailsService() {
81+
UserDetails userDetails = User.withDefaultPasswordEncoder()
82+
.username("user")
83+
.password("password")
84+
.roles("USER")
85+
.build();
86+
87+
return new InMemoryUserDetailsManager(userDetails);
88+
}
89+
90+
@Bean
91+
public RegisteredClientRepository registeredClientRepository() {
92+
// @formatter:off
10393
RegisteredClient oidcClient = RegisteredClient.withId(UUID.randomUUID().toString())
10494
.clientId("oidc-client")
10595
.clientSecret("{noop}oidc-client")
@@ -167,48 +157,49 @@ public RegisteredClientRepository registeredClientRepository() {
167157
).build();
168158

169159
// @formatter:on
170-
return new InMemoryRegisteredClientRepository(oidcClient, credentialsClient, pkceClient, opaqueClient);
171-
}
172-
173-
@Bean
174-
public AuthorizationServerSettings authorizationServerSettings() {
175-
return AuthorizationServerSettings.builder().build();
176-
}
177-
178-
@Bean
179-
public JwtDecoder jwtDecoder(JWKSource<SecurityContext> jwkSource) {
180-
return OAuth2AuthorizationServerConfiguration.jwtDecoder(jwkSource);
181-
}
182-
183-
@Bean
184-
JWKSource<SecurityContext> jwkSource() {
185-
RSAKey rsaKey = generateRsa();
186-
JWKSet jwkSet = new JWKSet(rsaKey);
187-
return new ImmutableJWKSet<>(jwkSet);
188-
}
189-
190-
private RSAKey generateRsa() {
191-
KeyPair keyPair = generateRsaKey();
192-
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
193-
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
194-
// @formatter:off
160+
return new InMemoryRegisteredClientRepository(oidcClient, credentialsClient, pkceClient, opaqueClient);
161+
}
162+
163+
@Bean
164+
public AuthorizationServerSettings authorizationServerSettings() {
165+
return AuthorizationServerSettings.builder().build();
166+
}
167+
168+
@Bean
169+
public JwtDecoder jwtDecoder(JWKSource<SecurityContext> jwkSource) {
170+
return OAuth2AuthorizationServerConfiguration.jwtDecoder(jwkSource);
171+
}
172+
173+
@Bean
174+
JWKSource<SecurityContext> jwkSource() {
175+
RSAKey rsaKey = generateRsa();
176+
JWKSet jwkSet = new JWKSet(rsaKey);
177+
return new ImmutableJWKSet<>(jwkSet);
178+
}
179+
180+
private RSAKey generateRsa() {
181+
KeyPair keyPair = generateRsaKey();
182+
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
183+
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
184+
// @formatter:off
195185
return new RSAKey.Builder(publicKey)
196186
.privateKey(privateKey)
197187
.keyID(UUID.randomUUID().toString())
198188
.build();
199189
// @formatter:on
200-
}
201-
202-
private KeyPair generateRsaKey() {
203-
KeyPair keyPair;
204-
try {
205-
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
206-
keyPairGenerator.initialize(2048);
207-
keyPair = keyPairGenerator.generateKeyPair();
208-
} catch (Exception ex) {
209-
throw new IllegalStateException(ex);
210-
}
211-
return keyPair;
212-
}
190+
}
191+
192+
private KeyPair generateRsaKey() {
193+
KeyPair keyPair;
194+
try {
195+
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
196+
keyPairGenerator.initialize(2048);
197+
keyPair = keyPairGenerator.generateKeyPair();
198+
}
199+
catch (Exception ex) {
200+
throw new IllegalStateException(ex);
201+
}
202+
return keyPair;
203+
}
213204

214205
}

my-samples/auth-server-01-start/src/test/java/com/chensoul/ClientCredentialsTests.java

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,22 @@
2020
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
2121
@AutoConfigureMockMvc
2222
public class ClientCredentialsTests {
23-
private static final String CLIENT_ID = "credentials-client";
24-
private static final String CLIENT_SECRET = "credentials-client";
2523

26-
private final ObjectMapper objectMapper = new ObjectMapper();
24+
private static final String CLIENT_ID = "credentials-client";
2725

28-
@Autowired
29-
private MockMvc mockMvc;
26+
private static final String CLIENT_SECRET = "credentials-client";
3027

31-
@LocalServerPort
32-
private long port;
28+
private final ObjectMapper objectMapper = new ObjectMapper();
3329

34-
@Test
35-
void performTokenRequestWhenValidClientCredentialsThenOk() throws Exception {
36-
// @formatter:off
30+
@Autowired
31+
private MockMvc mockMvc;
32+
33+
@LocalServerPort
34+
private long port;
35+
36+
@Test
37+
void performTokenRequestWhenValidClientCredentialsThenOk() throws Exception {
38+
// @formatter:off
3739
this.mockMvc.perform(post("/oauth2/token")
3840
.param("grant_type", "client_credentials")
3941
.param("scope", "read")
@@ -44,11 +46,11 @@ void performTokenRequestWhenValidClientCredentialsThenOk() throws Exception {
4446
.andExpect(jsonPath("$.scope").value("read"))
4547
.andExpect(jsonPath("$.token_type").value("Bearer"));
4648
// @formatter:on
47-
}
49+
}
4850

49-
@Test
50-
void performTokenRequestWhenMissingScopeThenOk() throws Exception {
51-
// @formatter:off
51+
@Test
52+
void performTokenRequestWhenMissingScopeThenOk() throws Exception {
53+
// @formatter:off
5254
this.mockMvc.perform(post("/oauth2/token")
5355
.param("grant_type", "client_credentials")
5456
.with(httpBasic(CLIENT_ID, CLIENT_SECRET)))
@@ -57,44 +59,44 @@ void performTokenRequestWhenMissingScopeThenOk() throws Exception {
5759
.andExpect(jsonPath("$.expires_in").isNumber())
5860
.andExpect(jsonPath("$.token_type").value("Bearer"));
5961
// @formatter:on
60-
}
62+
}
6163

62-
@Test
63-
void performTokenRequestWhenInvalidClientCredentialsThenUnauthorized() throws Exception {
64-
// @formatter:off
64+
@Test
65+
void performTokenRequestWhenInvalidClientCredentialsThenUnauthorized() throws Exception {
66+
// @formatter:off
6567
this.mockMvc.perform(post("/oauth2/token")
6668
.param("grant_type", "client_credentials")
6769
.param("scope", "read")
6870
.with(httpBasic("bad", "password")))
6971
.andExpect(status().isUnauthorized())
7072
.andExpect(jsonPath("$.error").value("invalid_client"));
7173
// @formatter:on
72-
}
74+
}
7375

74-
@Test
75-
void performTokenRequestWhenMissingGrantTypeThenUnauthorized() throws Exception {
76-
// @formatter:off
76+
@Test
77+
void performTokenRequestWhenMissingGrantTypeThenUnauthorized() throws Exception {
78+
// @formatter:off
7779
this.mockMvc.perform(post("/oauth2/token")
7880
.with(httpBasic("bad", "password")))
7981
.andExpect(status().isUnauthorized())
8082
.andExpect(jsonPath("$.error").value("invalid_client"));
8183
// @formatter:on
82-
}
84+
}
8385

84-
@Test
85-
void performTokenRequestWhenGrantTypeNotRegisteredThenBadRequest() throws Exception {
86-
// @formatter:off
86+
@Test
87+
void performTokenRequestWhenGrantTypeNotRegisteredThenBadRequest() throws Exception {
88+
// @formatter:off
8789
this.mockMvc.perform(post("/oauth2/token")
8890
.param("grant_type", "client_credentials")
8991
.with(httpBasic("oidc-client", "oidc-client")))
9092
.andExpect(status().isBadRequest())
9193
.andExpect(jsonPath("$.error").value("unauthorized_client"));
9294
// @formatter:on
93-
}
95+
}
9496

95-
@Test
96-
void performIntrospectionRequestWhenValidTokenThenOk() throws Exception {
97-
// @formatter:off
97+
@Test
98+
void performIntrospectionRequestWhenValidTokenThenOk() throws Exception {
99+
// @formatter:off
98100
this.mockMvc.perform(post("/oauth2/introspect")
99101
.param("token", getAccessToken())
100102
.with(httpBasic(CLIENT_ID, CLIENT_SECRET)))
@@ -110,21 +112,21 @@ void performIntrospectionRequestWhenValidTokenThenOk() throws Exception {
110112
.andExpect(jsonPath("$.sub").value(CLIENT_ID))
111113
.andExpect(jsonPath("$.token_type").value("Bearer"));
112114
// @formatter:on
113-
}
115+
}
114116

115-
@Test
116-
void performIntrospectionRequestWhenInvalidCredentialsThenUnauthorized() throws Exception {
117-
// @formatter:off
117+
@Test
118+
void performIntrospectionRequestWhenInvalidCredentialsThenUnauthorized() throws Exception {
119+
// @formatter:off
118120
this.mockMvc.perform(post("/oauth2/introspect")
119121
.param("token", getAccessToken())
120122
.with(httpBasic("bad", "password")))
121123
.andExpect(status().isUnauthorized())
122124
.andExpect(jsonPath("$.error").value("invalid_client"));
123125
// @formatter:on
124-
}
126+
}
125127

126-
private String getAccessToken() throws Exception {
127-
// @formatter:off
128+
private String getAccessToken() throws Exception {
129+
// @formatter:off
128130
MvcResult mvcResult = this.mockMvc.perform(post("http://localhost:" + port + "/oauth2/token")
129131
.param("grant_type", "client_credentials")
130132
.param("scope", "read")
@@ -134,11 +136,11 @@ private String getAccessToken() throws Exception {
134136
.andReturn();
135137
// @formatter:on
136138

137-
String tokenResponseJson = mvcResult.getResponse().getContentAsString();
138-
Map<String, Object> tokenResponse = this.objectMapper.readValue(tokenResponseJson, new TypeReference<>() {
139-
});
139+
String tokenResponseJson = mvcResult.getResponse().getContentAsString();
140+
Map<String, Object> tokenResponse = this.objectMapper.readValue(tokenResponseJson, new TypeReference<>() {
141+
});
140142

141-
return tokenResponse.get("access_token").toString();
142-
}
143+
return tokenResponse.get("access_token").toString();
144+
}
143145

144146
}

0 commit comments

Comments
 (0)