首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >正确配置弹簧安全oauth2

正确配置弹簧安全oauth2
EN

Stack Overflow用户
提问于 2018-04-22 20:17:45
回答 1查看 2.7K关注 0票数 0

我正在尝试用SpringSecurityOAuth2和jwt配置授权服务器。

我的主语是:

代码语言:javascript
运行
复制
@SpringBootApplication
@EnableResourceServer
public class Application {

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

我的安全配置:

代码语言:javascript
运行
复制
@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");
    }
}

我的auth服务器配置:

代码语言:javascript
运行
复制
@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);
    }

}

和访问当前用户的user服务:

代码语言:javascript
运行
复制
@RestController
public class UserController {

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

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

代码语言:javascript
运行
复制
gateway:secret@localhost:10101/oauth/token
grant_type : password
username : john
password : 123

我的回应是:

代码语言:javascript
运行
复制
{
    "access_token": "...access token...",
    "token_type": "bearer",
    "refresh_token": "...refresh token...",
    "expires_in": 43199,
    "scope": "GATEWAY",
    "jti": "...jti..."
}

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

代码语言:javascript
运行
复制
GET localhost:10101/users/me
Authorization : Bearer ...access token...

我得到了一个无效的答复。

但是,如果我在我的安全配置中有这一行:

代码语言:javascript
运行
复制
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.requestMatchers().antMatchers("/wtf");
    }

然后我得到适当的答复。

有人能解释一下这是怎么回事吗?为什么没有这个额外的行我的用户就不能被识别?

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-22 21:15:25

实际上这是春季保安的问题。解决方案在这个线程中:

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

应添加此注释,以便正确调用适配器:

代码语言:javascript
运行
复制
@Configuration 
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 
class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { 
    // ...
}

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49970346

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档