【问题标题】:Spring Boot Oauth2 logout endpointSpring Boot Oauth2 注销端点
【发布时间】:2017-03-26 13:50:48
【问题描述】:

我有一个 Spring Boot REST 应用程序,分为资源服务器和身份验证服务器 - 受无状态 Oauth2 安全性保护。

我正在使用 Spring Security 和 Oauth2 启动器:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
        </dependency>

资源服务器只是使用我的application.properties 中的这一行链接到身份验证服务器:

security.oauth2.resource.userInfoUri: http://localhost:9000/my-auth-server/user

身份验证服务器将使用凭据存储在数据库中,并具有以下配置:

@Configuration
@EnableAuthorizationServer
public class OAuth2Config extends AuthorizationServerConfigurerAdapter {

    @Autowired
    @Qualifier("userDetailsService")
    private UserDetailsService userDetailsService;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Value("${gigsterous.oauth.tokenTimeout:3600}")
    private int expiration;

    // password encryptor
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer configurer) throws Exception {
        configurer.authenticationManager(authenticationManager);
        configurer.userDetailsService(userDetailsService);
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory().withClient("gigsterous").secret("secret").accessTokenValiditySeconds(expiration)
                .scopes("read", "write").authorizedGrantTypes("password", "refresh_token").resourceIds("resource");
    }

}

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

    /**
     * Constructor disables the default security settings
     */
    public WebSecurityConfig() {
        super(true);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/login");
    }

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

}

一切正常,我可以获得访问令牌并使用它从我的资源服务器获取受保护的资源:

curl -X POST --user 'my-client-id:my-client-secret' -d 'grant_type=password&username=peter@hotmail.com&password=password' http://localhost:9000/my-auth-server/oauth/token

但是,我不知道如何处理注销(一旦用户决定注销,令牌就会失效)。我假设会提供一些端点来使令牌无效,还是我必须创建自己的端点来处理它?我不需要指定任何类型的 TokenStore bean,所以我不确定如何使当前令牌无效。如果有任何见解,我会很高兴 - 我发现的大多数教程都解释了如何使用会话或 JWT 令牌进行处理。

【问题讨论】:

    标签: java spring-boot oauth-2.0 spring-security-oauth2


    【解决方案1】:

    我遇到了这个问题,并在this post 上发布了解决方案。

    它基本上是在从客户端应用程序注销后重定向到授权服务器上的端点,然后以编程方式在那里注销。

    【讨论】:

    • 哇,这看起来很整洁。我将尝试将它实施到我的应用程序中,并在我得到它工作后接受这个答案。非常感谢!
    猜你喜欢
    • 2017-08-21
    • 2017-01-13
    • 2015-06-26
    • 1970-01-01
    • 2018-10-25
    • 2016-12-28
    • 2016-04-01
    • 2019-04-27
    • 1970-01-01
    相关资源
    最近更新 更多