【问题标题】:Correctly configure spring security oauth2正确配置spring security oauth2
【发布时间】:2018-10-02 20:46:12
【问题描述】:

我正在尝试使用 spring-security-oauth2 和 jwt 配置授权服务器。

我的主要:

@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);
    }

}

还有一个访问当前用户的网络服务:

@RestController
public class UserController {

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

现在,如果我发出这样的帖子请求:

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");
    }

然后我会得到适当的响应。

有人可以解释一下这是如何工作的吗?为什么没有这个附加行就无法识别我的用户?

谢谢。

【问题讨论】:

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


    【解决方案1】:

    其实是spring security的问题。 解决方案在这个线程中:

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

    应添加此注解以正确调用适配器:

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

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

    【讨论】:

    • 干得好,这使得遮阳篷更加有用,因为它现在包含更多信息。
    猜你喜欢
    • 2014-04-06
    • 2015-07-02
    • 2015-08-31
    • 2019-12-04
    • 2016-03-14
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    相关资源
    最近更新 更多