I'm pretty new with Spring, and I'm trying to build an OAuth Server using spring-security-oauth2.
I mainly refered to the sample and a tutorial given by spring.io.
https://github.com/spring-projects/spring-security-oauth/tree/master/samples/oauth2 http://spring.io/guides/tutorials/spring-boot-oauth2/
However, I face some problems about HttpSecurity configuration.
My folder structure is as follow.
├─java │ └─com │ └─example │ Greeting.java │ GreetingController.java │ MvcConfig.java │ OAuth2ServerConfig.java │ SecurityConfig.java │ SocialApplication.java │ └─resources │ application.yml │ └─templates hello.html home.html login.html I add some HttpSecurity configuration in SecurityConfig.java
@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter{ @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/", "/home").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .permitAll() .and() .logout() .permitAll(); } } OK, it works fine. However, I want to protect the greeting api (which is a simple rest api I just copied from another demo) in my Resource Server. So I add some HttpSecurity configuration in OAuth2ServerConfig.java
@Configuration @EnableResourceServer protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter { @Override public void configure(ResourceServerSecurityConfigurer resources) throws Exception { resources.resourceId(RESOURCE_ID); } @Override public void configure(HttpSecurity http) throws Exception { // @formatter:off http .requestMatchers().antMatchers("/greeting") .and() .authorizeRequests() .anyRequest().access("#oauth2.hasScope('scope1')"); // @formatter:on } } It seems that I only protect /greeting. However, when this was done, I can't even access / and /login. It said that Full authentication is required to access this resource.
Did I miss some configuration or do anything wrong?