I came across this project in order to fix a issue which the server is responding with 401 unautorized to a OPTIONS request.
I looked in the project and ( It is a Spring 5 project bundled as a ear not a boot ) found that there is a CORS filter. Then I looked into security configs and found that there is two.
//@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private CustomUserDetailsService customUserDetailsService;
@Bean
public CustomDaoAuthenticationProvider authenticationProvier() {
CustomDaoAuthenticationProvider customProvider = new CustomDaoAuthenticationProvider();
customProvider.setUserDetailsService(customUserDetailsService);
customProvider.setPasswordEncoder(passwordEncoder());
return customProvider;
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvier());
}
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(final HttpSecurity http) throws Exception {
http
.cors()
.and()
.csrf().disable()
.anonymous().disable()
.authorizeRequests()
.antMatchers("/**").permitAll();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(10);
}
}
The second one is
@Configuration
@EnableResourceServer
@EnableWebSecurity
public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(final HttpSecurity http) throws Exception {
http.anonymous().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.csrf().disable().authorizeRequests()
.antMatchers(HttpMethod.GET, "/somepath/**").access("#oauth2.hasScope('some_scope') "
+ "and hasAnyRole('role_1','r')")
....
.anyRequest().authenticated();
}
@Override
public void configure(final ResourceServerSecurityConfigurer config) {
config.tokenServices(tokenServices());
}
@Bean
public TokenStore tokenStore() {
return new JwtTokenStore(accessTokenConverter());
}
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
final JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
ClassPathResource resource = new ClassPathResource("id_rsa.pub");
String publicKey = null;
try {
publicKey = new String(FileCopyUtils.copyToByteArray(resource.getInputStream()));
} catch (IOException e) {
throw new RuntimeException(e);
}
converter.setVerifierKey(publicKey);
return converter;
}
@Bean
@Primary
public DefaultTokenServices tokenServices() {
final DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
defaultTokenServices.setTokenStore(tokenStore());
return defaultTokenServices;
}
}
After reading questions related to this what I figured it that OAuth2ResourceServerConfig gets precedence over WebSecurityConfig ( question here ).
So in the current set is it checking for both token and the password? If I increase the precedence of WebSecurityConfig will it solve the issue. If I have misunderstood anything here please fix it for me.