公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

本文介绍了正确配置Spring安全OAuth2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Spring-Security-OAuth2和JWT配置授权服务器。

我的Main:

@SpringBootApplication
@EnableResourceServer
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

我的安全配置:

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    @Bean
    public AuthenticationManager    authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .inMemoryAuthentication()
                .withUser("john").password("123").roles("USER").authorities("FOO_READ");
    }
}

我的身份验证服务器配置:

@Configuration
@EnableAuthorizationServer
public class AuthServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("gateway")
                .secret("secret")
                .authorizedGrantTypes("refresh_token", "password")
                .scopes("GATEWAY")
                .autoApprove(true) ;
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.tokenEnhancer(jwtTokenEnhancer()).authenticationManager(authenticationManager);
    }

}

和用于访问当前用户的Web服务:

@RestController
public class UserController {

    @GetMapping("/users/me")
    public Principal user(Principal principal) {
        return principal;
    }
}

现在,如果我这样做POST请求:

gateway:secret@localhost:10101/oauth/token
grant_type : password
username : john
password : 123

我得到的回复如下:

{
    "access_token": "...access token...",
    "token_type": "bearer",
    "refresh_token": "...refresh token...",
    "expires_in": 43199,
    "scope": "GATEWAY",
    "jti": "...jti..."
}

如果我使用访问令牌呼叫我的用户WS:

GET localhost:10101/users/me
Authorization : Bearer ...access token...

我获得空响应。

但如果我的安全配置中有这几行:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.requestMatchers().antMatchers("/wtf");
    }

然后我获得相应的响应。

有人能给我解释一下这是怎么工作的吗?如果没有这一附加行,为什么无法识别我的用户?

谢谢。

推荐答案

实际上是Spring安全问题。
解决方案在此帖子中:

https://github.com/spring-projects/spring-security-oauth/issues/980

应添加此批注才能正确调用适配器:

@Configuration 
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 
class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { 
    // ...
}

另一个解决方案是实现ResourceServerConfigurerAdapter而不是WebSecurityConfigurerAdapter。

这篇关于正确配置Spring安全OAuth2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,

分享到:
标签:OAuth2 Spring 正确 配置
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定