Skip to content

Commit c9651d5

Browse files
Some OAuthLogin configuration improvements
1 parent 0d82607 commit c9651d5

File tree

3 files changed

+12
-22
lines changed

3 files changed

+12
-22
lines changed

spring-lemon-commons-reactive/src/main/java/com/naturalprogrammer/spring/lemon/commonsreactive/security/LemonCommonsReactiveSecurityConfig.java

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
99
import org.springframework.security.config.web.server.SecurityWebFiltersOrder;
1010
import org.springframework.security.config.web.server.ServerHttpSecurity;
11+
import org.springframework.security.core.AuthenticationException;
1112
import org.springframework.security.web.server.SecurityWebFilterChain;
1213
import org.springframework.security.web.server.ServerAuthenticationEntryPoint;
14+
import org.springframework.security.web.server.WebFilterExchange;
1315
import org.springframework.security.web.server.authentication.AuthenticationWebFilter;
1416
import org.springframework.security.web.server.authentication.ServerAuthenticationConverter;
1517
import org.springframework.security.web.server.authentication.ServerAuthenticationFailureHandler;
@@ -44,8 +46,8 @@ public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http)
4446
return http
4547
.securityContextRepository(NoOpServerSecurityContextRepository.getInstance())
4648
.exceptionHandling()
47-
.accessDeniedHandler(accessDeniedHandler())
48-
.authenticationEntryPoint(authenticationEntryPoint())
49+
.accessDeniedHandler((exchange, exception) -> Mono.error(exception))
50+
.authenticationEntryPoint((exchange, exception) -> Mono.error(exception))
4951
.and()
5052
.cors()
5153
.and()
@@ -86,7 +88,7 @@ protected AuthenticationWebFilter tokenAuthenticationFilter() {
8688

8789
AuthenticationWebFilter filter = new AuthenticationWebFilter(tokenAuthenticationManager());
8890
filter.setServerAuthenticationConverter(tokenAuthenticationConverter());
89-
filter.setAuthenticationFailureHandler(authenticationFailureHandler());
91+
filter.setAuthenticationFailureHandler((exchange, exception) -> Mono.error(exception));
9092

9193
return filter;
9294
}
@@ -136,20 +138,4 @@ protected ServerAuthenticationConverter tokenAuthenticationConverter() {
136138
return Mono.just(new UsernamePasswordAuthenticationToken(null, authorization.substring(LecUtils.TOKEN_PREFIX_LENGTH)));
137139
};
138140
}
139-
140-
protected ServerAuthenticationFailureHandler authenticationFailureHandler() {
141-
142-
return (webFilterExchange, exception) -> Mono.error(exception);
143-
}
144-
145-
protected ServerAccessDeniedHandler accessDeniedHandler() {
146-
147-
return (exchange, exception) -> Mono.error(exception);
148-
}
149-
150-
protected ServerAuthenticationEntryPoint authenticationEntryPoint() {
151-
152-
return (exchange, exception) -> Mono.error(exception);
153-
}
154-
155141
}

spring-lemon-jpa/src/main/java/com/naturalprogrammer/spring/lemon/security/LemonJpaSecurityConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.apache.commons.logging.LogFactory;
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
7+
import org.springframework.security.config.http.SessionCreationPolicy;
78
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
89
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
910

spring-lemon-reactive/src/main/java/com/naturalprogrammer/spring/lemonreactive/security/LemonReactiveSecurityConfig.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.springframework.security.core.userdetails.UsernameNotFoundException;
1010
import org.springframework.security.web.server.WebFilterExchange;
1111
import org.springframework.security.web.server.authentication.WebFilterChainServerAuthenticationSuccessHandler;
12+
import org.springframework.security.web.server.context.NoOpServerSecurityContextRepository;
1213

1314
import com.naturalprogrammer.spring.lemon.commons.LemonProperties;
1415
import com.naturalprogrammer.spring.lemon.commons.security.BlueTokenService;
@@ -49,8 +50,9 @@ public LemonReactiveSecurityConfig(BlueTokenService blueTokenService,
4950
protected void formLogin(ServerHttpSecurity http) {
5051

5152
http.formLogin()
53+
.securityContextRepository(NoOpServerSecurityContextRepository.getInstance())
5254
.loginPage(loginPage()) // Should be "/login" by default, but not providing that overwrites our AuthenticationFailureHandler, because this is called later
53-
.authenticationFailureHandler(authenticationFailureHandler())
55+
.authenticationFailureHandler((exchange, exception) -> Mono.error(exception))
5456
.authenticationSuccessHandler(new WebFilterChainServerAuthenticationSuccessHandler());
5557
}
5658

@@ -69,9 +71,10 @@ protected String loginPage() {
6971
protected void oauth2Login(ServerHttpSecurity http) {
7072

7173
http.oauth2Login()
74+
.securityContextRepository(NoOpServerSecurityContextRepository.getInstance())
7275
.authorizedClientRepository(new ReactiveCookieServerOAuth2AuthorizedClientRepository(properties))
7376
.authenticationSuccessHandler(reactiveOAuth2AuthenticationSuccessHandler)
74-
.authenticationFailureHandler(this::onAuthenticationFailure);
77+
.authenticationFailureHandler(this::onOauth2AuthenticationFailure);
7578
}
7679

7780
@Override
@@ -88,7 +91,7 @@ protected Mono<UserDto> fetchUserDto(JWTClaimsSet claims) {
8891
.map(AbstractMongoUser::toUserDto);
8992
}
9093

91-
protected Mono<Void> onAuthenticationFailure(WebFilterExchange webFilterExchange, AuthenticationException exception) {
94+
protected Mono<Void> onOauth2AuthenticationFailure(WebFilterExchange webFilterExchange, AuthenticationException exception) {
9295

9396
ReactiveCookieServerOAuth2AuthorizedClientRepository.deleteCookies(webFilterExchange.getExchange(),
9497
LecUtils.AUTHORIZATION_REQUEST_COOKIE_NAME,

0 commit comments

Comments
 (0)