【问题标题】:How can I protect url paths with only client's ID and secret?如何仅使用客户端 ID 和密码保护 url 路径?
【发布时间】:2015-04-14 00:16:29
【问题描述】:

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

像 www.mywebsite.com/api/doStuff [GET/POST] 这样的页面需要有效的 OAuth 令牌来保护,但是我如何使用 保护像 www.mywebsite.com/api/signup [POST] 这样的页面仅 客户 IDsecret 带有标头 Authorization: Basic [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);
}

感谢您的帮助!

【问题讨论】:

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


    【解决方案1】:

    如果您想对某些资源使用基本身份验证,您最好创建一个单独的过滤器链(即新的WebSecurityConfigurerAdapter)并在其中设置规则以仅匹配您想要以这种方式保护的路径.

    【讨论】:

      猜你喜欢
      • 2017-10-18
      • 1970-01-01
      • 2015-01-06
      • 1970-01-01
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多