如何仅使用客户端的ID和密码来保护URL路径?

时间:2015-02-13 02:08:03

标签: java spring spring-mvc spring-security spring-security-oauth2

我尝试使用Spring配置RESTFul Web服务,并且我已经配置了OAuth。所以我有几页需要用户的OAuth令牌。但是,我有一些页面可用于匿名访问,例如注册或忘记密码。

www.mywebsite.com/api/doStuff [GET / POST]等页面受到有效OAuth令牌的保护,但我如何用保护www.mywebsite.com/api/signup [POST]等网页?仅 客户的ID 机密,标题为授权:基本[Base64EncodedString]

我不想要那些"不安全的页面"除了客户端之外的任何人都可以访问,例如移动应用程序(iOS / Android)。

这是我的ResourceServerConfigurerAdapter类中的configure(HttpSecurity)方法

public void configure(HttpSecurity http) throws Exception {
http
    .requestMatchers().antMatchers("/api/**")
.and()
    .authorizeRequests()
    .antMatchers("/api/doStuff", "/api/getOtherStuff").access("#oauth2.hasScope('read') and hasRole('ROLE_USER')")
    .antMatchers("/api/signup").access("#oauth2.isClient()") // Not working like I thought...
.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

如果你想为某些资源使用基本身份验证,你可能最好创建一个单独的过滤器链(即新的WebSecurityConfigurerAdapter)并在那里设置规则以仅匹配你想要保护的路径那样。