|
|
|
|
@ -25,6 +25,9 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@@ -25,6 +25,9 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
|
|
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
|
|
|
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
|
|
|
|
import org.springframework.security.config.test.SpringTestRule; |
|
|
|
|
import org.springframework.security.core.authority.AuthorityUtils; |
|
|
|
|
import org.springframework.security.core.userdetails.AuthenticationUserDetailsService; |
|
|
|
|
import org.springframework.security.core.userdetails.User; |
|
|
|
|
import org.springframework.security.web.authentication.preauth.j2ee.J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource; |
|
|
|
|
import org.springframework.security.web.authentication.preauth.j2ee.J2eePreAuthenticatedProcessingFilter; |
|
|
|
|
import org.springframework.test.web.servlet.MockMvc; |
|
|
|
|
@ -125,4 +128,112 @@ public class JeeConfigurerTests {
@@ -125,4 +128,112 @@ public class JeeConfigurerTests {
|
|
|
|
|
// @formatter:on
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void requestWhenJeeMappableRolesInLambdaThenAuthenticatedWithMappableRoles() throws Exception { |
|
|
|
|
this.spring.register(JeeMappableRolesConfig.class).autowire(); |
|
|
|
|
Principal user = mock(Principal.class); |
|
|
|
|
when(user.getName()).thenReturn("user"); |
|
|
|
|
|
|
|
|
|
this.mvc.perform(get("/") |
|
|
|
|
.principal(user) |
|
|
|
|
.with(request -> { |
|
|
|
|
request.addUserRole("ROLE_ADMIN"); |
|
|
|
|
request.addUserRole("ROLE_USER"); |
|
|
|
|
return request; |
|
|
|
|
})) |
|
|
|
|
.andExpect(authenticated().withRoles("USER")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@EnableWebSecurity |
|
|
|
|
public static class JeeMappableRolesConfig extends WebSecurityConfigurerAdapter { |
|
|
|
|
@Override |
|
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
|
// @formatter:off
|
|
|
|
|
http |
|
|
|
|
.authorizeRequests() |
|
|
|
|
.anyRequest().hasRole("USER") |
|
|
|
|
.and() |
|
|
|
|
.jee(jee -> |
|
|
|
|
jee |
|
|
|
|
.mappableRoles("USER") |
|
|
|
|
); |
|
|
|
|
// @formatter:on
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void requestWhenJeeMappableAuthoritiesInLambdaThenAuthenticatedWithMappableAuthorities() throws Exception { |
|
|
|
|
this.spring.register(JeeMappableAuthoritiesConfig.class).autowire(); |
|
|
|
|
Principal user = mock(Principal.class); |
|
|
|
|
when(user.getName()).thenReturn("user"); |
|
|
|
|
|
|
|
|
|
this.mvc.perform(get("/") |
|
|
|
|
.principal(user) |
|
|
|
|
.with(request -> { |
|
|
|
|
request.addUserRole("ROLE_ADMIN"); |
|
|
|
|
request.addUserRole("ROLE_USER"); |
|
|
|
|
return request; |
|
|
|
|
})) |
|
|
|
|
.andExpect(authenticated().withAuthorities(AuthorityUtils.createAuthorityList("ROLE_USER"))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@EnableWebSecurity |
|
|
|
|
public static class JeeMappableAuthoritiesConfig extends WebSecurityConfigurerAdapter { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
|
// @formatter:off
|
|
|
|
|
http |
|
|
|
|
.authorizeRequests() |
|
|
|
|
.anyRequest().hasRole("USER") |
|
|
|
|
.and() |
|
|
|
|
.jee(jee -> |
|
|
|
|
jee |
|
|
|
|
.mappableAuthorities("ROLE_USER") |
|
|
|
|
); |
|
|
|
|
// @formatter:on
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void requestWhenCustomAuthenticatedUserDetailsServiceInLambdaThenCustomAuthenticatedUserDetailsServiceUsed() |
|
|
|
|
throws Exception { |
|
|
|
|
this.spring.register(JeeCustomAuthenticatedUserDetailsServiceConfig.class).autowire(); |
|
|
|
|
Principal user = mock(Principal.class); |
|
|
|
|
User userDetails = new User("user", "N/A", true, true, true, true, |
|
|
|
|
AuthorityUtils.createAuthorityList("ROLE_USER")); |
|
|
|
|
when(user.getName()).thenReturn("user"); |
|
|
|
|
when(JeeCustomAuthenticatedUserDetailsServiceConfig.authenticationUserDetailsService.loadUserDetails(any())) |
|
|
|
|
.thenReturn(userDetails); |
|
|
|
|
|
|
|
|
|
this.mvc.perform(get("/") |
|
|
|
|
.principal(user) |
|
|
|
|
.with(request -> { |
|
|
|
|
request.addUserRole("ROLE_ADMIN"); |
|
|
|
|
request.addUserRole("ROLE_USER"); |
|
|
|
|
return request; |
|
|
|
|
})) |
|
|
|
|
.andExpect(authenticated().withRoles("USER")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@EnableWebSecurity |
|
|
|
|
public static class JeeCustomAuthenticatedUserDetailsServiceConfig extends WebSecurityConfigurerAdapter { |
|
|
|
|
static AuthenticationUserDetailsService authenticationUserDetailsService = |
|
|
|
|
mock(AuthenticationUserDetailsService.class); |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
|
// @formatter:off
|
|
|
|
|
http |
|
|
|
|
.authorizeRequests() |
|
|
|
|
.anyRequest().hasRole("USER") |
|
|
|
|
.and() |
|
|
|
|
.jee(jee -> |
|
|
|
|
jee |
|
|
|
|
.authenticatedUserDetailsService(authenticationUserDetailsService) |
|
|
|
|
); |
|
|
|
|
// @formatter:on
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|