Skip to content

Commit b12dab7

Browse files
committed
Custom error handler
1 parent bb2c455 commit b12dab7

File tree

4 files changed

+45
-156
lines changed

4 files changed

+45
-156
lines changed

spring-security-auth-server/src/main/java/com/stacktips/app/config/AuthorizationServerConfig.java

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,14 @@
1515
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
1616
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
1717
import org.springframework.security.oauth2.server.authorization.config.annotation.web.configuration.OAuth2AuthorizationServerConfiguration;
18-
import org.springframework.security.oauth2.server.authorization.config.annotation.web.configurers.OAuth2AuthorizationServerConfigurer;
1918
import org.springframework.security.oauth2.server.authorization.settings.AuthorizationServerSettings;
2019
import org.springframework.security.oauth2.server.authorization.settings.ClientSettings;
2120
import org.springframework.security.oauth2.server.authorization.settings.TokenSettings;
2221
import org.springframework.security.web.SecurityFilterChain;
23-
import org.springframework.security.web.util.matcher.RequestMatcher;
2422

2523
import java.time.Duration;
2624
import java.util.UUID;
2725

28-
import static org.springframework.security.config.Customizer.withDefaults;
29-
3026
@Configuration
3127
public class AuthorizationServerConfig {
3228

@@ -78,44 +74,30 @@ public JwtEncoder jwtEncoder(JWKSource<SecurityContext> jwkSource) {
7874
return new NimbusJwtEncoder(jwkSource);
7975
}
8076

81-
// OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
82-
// new OAuth2AuthorizationServerConfigurer();
83-
// RequestMatcher endpointsMatcher = authorizationServerConfigurer
84-
// .getEndpointsMatcher();
85-
//
86-
// http
87-
// .requestMatcher(endpointsMatcher)
88-
// .authorizeRequests(authorizeRequests ->
89-
// authorizeRequests.anyRequest().authenticated()
90-
// )
91-
// .csrf(csrf -> csrf.ignoringRequestMatchers("/auth/token"))
92-
// .apply(authorizationServerConfigurer);
93-
//
94-
// return http.build();
9577

9678
@Bean
9779
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
9880
OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http);
9981
return http.formLogin(Customizer.withDefaults()).build();
10082
}
10183

102-
// @Bean
103-
// public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
104-
//
105-
// OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
106-
// new OAuth2AuthorizationServerConfigurer();
107-
// OAuth2AuthorizationServerConfigurer serverConfigurer = authorizationServerConfigurer
108-
// .tokenEndpoint(tokenEndpoint -> tokenEndpoint.accessTokenResponseHandler(null));
109-
//
110-
// http.with(serverConfigurer, Customizer.withDefaults())
111-
// .authorizeHttpRequests(authorizeRequests -> authorizeRequests.anyRequest().authenticated())
112-
// .csrf(csrf -> csrf.ignoringRequestMatchers("/oauth2/token"));
113-
//// .formLogin(withDefaults());
114-
// return http.build();
115-
//
116-
// }
117-
11884

85+
/*
86+
// Custom exception response
87+
@Bean
88+
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http,
89+
CustomAuthenticationEntryPoint customAuthenticationEntryPoint) throws Exception {
90+
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
91+
new OAuth2AuthorizationServerConfigurer();
92+
RequestMatcher endpointsMatcher = authorizationServerConfigurer
93+
.getEndpointsMatcher();
94+
95+
http.with(authorizationServerConfigurer, Customizer.withDefaults())
96+
.authorizeHttpRequests(authorizeRequests -> authorizeRequests.anyRequest().authenticated())
97+
.csrf(csrf -> csrf.ignoringRequestMatchers("/oauth2/token"));
98+
.exceptionHandling((ex) -> ex.authenticationEntryPoint(customAuthenticationEntryPoint));
99+
return http.build();
100+
}*/
119101

120102
}
121103

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.stacktips.app.config;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import jakarta.servlet.http.HttpServletRequest;
5+
import jakarta.servlet.http.HttpServletResponse;
6+
import org.springframework.security.core.AuthenticationException;
7+
import org.springframework.security.web.AuthenticationEntryPoint;
8+
import org.springframework.stereotype.Component;
9+
10+
import java.io.IOException;
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
14+
@Component
15+
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
16+
17+
private final ObjectMapper objectMapper = new ObjectMapper();
18+
19+
@Override
20+
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
21+
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
22+
response.setContentType("application/json");
23+
24+
Map<String, Object> data = new HashMap<>();
25+
data.put("error", authException.getMessage());
26+
data.put("message", "Custom error message: Invalid client authentication");
27+
response.getOutputStream().println(objectMapper.writeValueAsString(data));
28+
}
29+
}

spring-security-auth-server/src/main/java/com/stacktips/app/config/WebSecurityConfig.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,6 @@ public UserDetailsService userDetailsService() {
2727
);
2828
}
2929

30-
//
31-
// public ClientDetailsService clientDetailsService() {
32-
// return new ClientDetailsService() {
33-
// @Override
34-
// public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
35-
// BaseClientDetails details = new BaseClientDetails();
36-
// details.setClientId(clientId);
37-
// details.setAuthorizedGrantTypes(Arrays.asList("authorization_code") );
38-
// details.setScope(Arrays.asList("read, trust"));
39-
// details.setRegisteredRedirectUri(Collections.singleton("http://anywhere.com"));
40-
// details.setResourceIds(Arrays.asList("oauth2-resource"));
41-
// Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
42-
// authorities.add(new SimpleGrantedAuthority("ROLE_CLIENT"));
43-
// details.setAuthorities(authorities);
44-
// return details;
45-
// }
46-
// };
47-
// } //*/
48-
49-
5030
@Bean
5131
public AuthenticationManager authenticationManager(
5232
final AuthenticationConfiguration authenticationConfiguration) throws Exception {

spring-security-auth-server/src/test/java/com/stacktips/app/OAuth2FlowTest.java

Lines changed: 0 additions & 102 deletions
This file was deleted.

0 commit comments

Comments
 (0)