Skip to content

Commit 192feef

Browse files
authored
Merge pull request #4309 from bogdan-sava/ui
[ui-chassis-server] refactor LoginFilter
2 parents 3183820 + 416a17b commit 192feef

File tree

5 files changed

+70
-19
lines changed

5 files changed

+70
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* SPDX-License-Identifier: Apache-2.0 */
2+
/* Copyright Contributors to the ODPi Egeria project. */
3+
package org.odpi.openmetadata.userinterface.uichassis.springboot.auth;
4+
5+
import org.springframework.security.authentication.BadCredentialsException;
6+
import org.springframework.security.core.AuthenticationException;
7+
8+
/**
9+
* Handles AuthenticationException for different instances of WebSecurityConfigurerAdapter used for different
10+
* authentication mechanism used
11+
*/
12+
public interface AuthenticationExceptionHandler {
13+
14+
/**
15+
*
16+
* @param e the AuthenticationException thrown by authentication attempt
17+
* @return whether or not is an bad credentials related exception
18+
*/
19+
boolean isBadCredentials(AuthenticationException e);
20+
}

open-metadata-implementation/user-interfaces/ui-chassis/ui-chassis-spring/src/main/java/org/odpi/openmetadata/userinterface/uichassis/springboot/auth/LoginFilter.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
77
import org.springframework.http.HttpStatus;
8-
import org.springframework.ldap.InvalidSearchFilterException;
98
import org.springframework.security.authentication.AuthenticationManager;
10-
import org.springframework.security.authentication.BadCredentialsException;
11-
import org.springframework.security.authentication.InsufficientAuthenticationException;
129
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
1310
import org.springframework.security.core.Authentication;
1411
import org.springframework.security.core.AuthenticationException;
@@ -26,37 +23,37 @@ public class LoginFilter extends AbstractAuthenticationProcessingFilter {
2623
private static final String USERNAME = "username";
2724
private static final String PASSWORD = "password";
2825
private final AuthService authenticationService;
26+
private final AuthenticationExceptionHandler authenticationExceptionHandler;
2927

3028
Logger log = LoggerFactory.getLogger(this.getClass());
3129

32-
protected LoginFilter(String urlMapping, AuthenticationManager authenticationManager, AuthService authenticationService) {
30+
protected LoginFilter(String urlMapping,
31+
AuthenticationManager authenticationManager,
32+
AuthService authenticationService,
33+
AuthenticationExceptionHandler authenticationExceptionHandler) {
3334
super(new AntPathRequestMatcher(urlMapping));
3435
setAuthenticationManager(authenticationManager);
3536
this.authenticationService = authenticationService;
37+
this.authenticationExceptionHandler = authenticationExceptionHandler;
3638
}
3739

3840
@Override
3941
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
40-
throws AuthenticationException,IOException {
42+
throws AuthenticationException {
4143

4244
String username = request.getParameter(USERNAME);
4345
String password = request.getParameter(PASSWORD);
4446
Authentication authentication = getAuthenticationManager()
4547
.authenticate(new UsernamePasswordAuthenticationToken( username, password));
46-
47-
if(authentication.getAuthorities().isEmpty()){
48-
log.warn("NO roles for user: {}", request.getParameter(USERNAME));
49-
response.sendError(HttpStatus.FORBIDDEN.value(), HttpStatus.FORBIDDEN.getReasonPhrase());
50-
}
5148
return authentication;
5249
}
5350

5451
@Override
5552
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response,
5653
AuthenticationException failed) throws IOException {
57-
log.info("Unsuccessful Authentication");
58-
if(failed instanceof BadCredentialsException || failed.getCause() instanceof InvalidSearchFilterException) {
59-
log.warn("Bad credentials UNSUCCESSFUL AUTHENTICATION for user: {}", request.getParameter(USERNAME));
54+
log.info("UNSUCCESSFUL Authentication");
55+
if( authenticationExceptionHandler.isBadCredentials(failed) ) {
56+
log.warn("Bad credentials for user: {}", request.getParameter(USERNAME));
6057
response.sendError(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase());
6158
} else {
6259
log.warn("ERROR AUTHENTICATION for user: {}", request.getParameter(USERNAME), failed);
@@ -66,10 +63,13 @@ protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServle
6663

6764
@Override
6865
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,
69-
FilterChain chain, Authentication authentication) {
70-
log.info("Successful Authentication");
66+
FilterChain chain, Authentication authentication) throws IOException {
67+
log.info("SUCCESSFUL Authentication for user {}", request.getParameter(USERNAME));
7168
authenticationService.addAuthentication(request, response, authentication);
7269
SecurityContextHolder.getContext().setAuthentication(authentication);
73-
70+
if(authentication.getAuthorities().isEmpty()){
71+
log.warn("NO roles for user: {}", request.getParameter(USERNAME));
72+
response.sendError(HttpStatus.FORBIDDEN.value(), HttpStatus.FORBIDDEN.getReasonPhrase());
73+
}
7474
}
7575
}

open-metadata-implementation/user-interfaces/ui-chassis/ui-chassis-spring/src/main/java/org/odpi/openmetadata/userinterface/uichassis/springboot/auth/SecurityConfig.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import org.springframework.security.ldap.userdetails.InetOrgPersonContextMapper;
1111
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
1212

13-
public class SecurityConfig extends WebSecurityConfigurerAdapter {
13+
public abstract class SecurityConfig extends WebSecurityConfigurerAdapter {
1414

1515
@Autowired
1616
private AuthService authService;
@@ -34,8 +34,11 @@ protected void configure(HttpSecurity http) throws Exception {
3434
.and()
3535
.addFilterBefore(new AuthFilter(authService), UsernamePasswordAuthenticationFilter.class)
3636
.addFilterBefore(new LoggingRequestFilter("/api/auth/login"), UsernamePasswordAuthenticationFilter.class)
37-
.addFilterBefore(new LoginFilter("/api/auth/login", authenticationManager(), authService),
38-
UsernamePasswordAuthenticationFilter.class)
37+
.addFilterBefore(
38+
new LoginFilter("/api/auth/login",
39+
authenticationManager(),
40+
authService,
41+
getAuthenticationExceptionHandler()),UsernamePasswordAuthenticationFilter.class)
3942
;
4043
}
4144

@@ -49,4 +52,6 @@ public AuthenticationManager authenticationManagerBean() throws Exception {
4952
public InetOrgPersonContextMapper userContextMapper() {
5053
return new InetOrgPersonContextMapper();
5154
}
55+
56+
protected abstract AuthenticationExceptionHandler getAuthenticationExceptionHandler();
5257
}

open-metadata-implementation/user-interfaces/ui-chassis/ui-chassis-spring/src/main/java/org/odpi/openmetadata/userinterface/uichassis/springboot/auth/db/DbSecurityConfig.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22
/* Copyright Contributors to the ODPi Egeria project. */
33
package org.odpi.openmetadata.userinterface.uichassis.springboot.auth.db;
44

5+
import org.odpi.openmetadata.userinterface.uichassis.springboot.auth.AuthenticationExceptionHandler;
56
import org.odpi.openmetadata.userinterface.uichassis.springboot.auth.SecurityConfig;
67
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.beans.factory.annotation.Qualifier;
89
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
910
import org.springframework.context.annotation.Configuration;
1011
import org.springframework.core.Ordered;
1112
import org.springframework.core.annotation.Order;
13+
import org.springframework.ldap.InvalidSearchFilterException;
14+
import org.springframework.security.authentication.BadCredentialsException;
1215
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
1316
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
17+
import org.springframework.security.core.AuthenticationException;
1418
import org.springframework.security.core.userdetails.UserDetailsService;
1519
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
1620

@@ -29,4 +33,13 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
2933
auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
3034
}
3135

36+
@Override
37+
protected AuthenticationExceptionHandler getAuthenticationExceptionHandler() {
38+
return new AuthenticationExceptionHandler() {
39+
@Override
40+
public boolean isBadCredentials(AuthenticationException e) {
41+
return e instanceof BadCredentialsException;
42+
}
43+
};
44+
}
3245
}

open-metadata-implementation/user-interfaces/ui-chassis/ui-chassis-spring/src/main/java/org/odpi/openmetadata/userinterface/uichassis/springboot/auth/ldap/LdapSecurityConfig.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22
/* Copyright Contributors to the ODPi Egeria project. */
33
package org.odpi.openmetadata.userinterface.uichassis.springboot.auth.ldap;
44

5+
import org.odpi.openmetadata.userinterface.uichassis.springboot.auth.AuthenticationExceptionHandler;
56
import org.odpi.openmetadata.userinterface.uichassis.springboot.auth.SecurityConfig;
67
import org.springframework.beans.factory.annotation.Value;
78
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
89
import org.springframework.context.annotation.Configuration;
910
import org.springframework.core.Ordered;
1011
import org.springframework.core.annotation.Order;
12+
import org.springframework.ldap.InvalidSearchFilterException;
13+
import org.springframework.security.authentication.BadCredentialsException;
1114
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
1215
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
16+
import org.springframework.security.core.AuthenticationException;
1317

1418

1519
@EnableWebSecurity
@@ -67,4 +71,13 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
6771

6872
}
6973

74+
@Override
75+
protected AuthenticationExceptionHandler getAuthenticationExceptionHandler() {
76+
return new AuthenticationExceptionHandler() {
77+
@Override
78+
public boolean isBadCredentials(AuthenticationException e) {
79+
return e instanceof BadCredentialsException || e.getCause() instanceof InvalidSearchFilterException;
80+
}
81+
};
82+
}
7083
}

0 commit comments

Comments
 (0)