【发布时间】:2016-03-14 20:57:38
【问题描述】:
我对 Spring 很陌生,我正在尝试使用 spring-security-oauth2 构建一个 OAuth 服务器。
我主要参考了spring.io给出的示例和教程。
https://github.com/spring-projects/spring-security-oauth/tree/master/samples/oauth2 http://spring.io/guides/tutorials/spring-boot-oauth2/
但是,我在HttpSecurity 配置方面遇到了一些问题。
我的文件夹结构如下。
├─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
我在SecurityConfig.java中添加了一些HttpSecurity配置
@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();
}
}
好的,它工作正常。但是,我想保护Resource Server 中的greeting api(这是我刚刚从另一个演示中复制的一个简单的rest api)。
所以我在OAuth2ServerConfig.java中添加了一些HttpSecurity配置
@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
}
}
看来我只保护/greeting。但是,完成此操作后,我什至无法访问/ 和/login。它说Full authentication is required to access this resource。
我是否错过了某些配置或做错了什么?
【问题讨论】:
标签: spring spring-security spring-security-oauth2