首页 文章

Spring Boot Oauth2注销 endpoints

提问于
浏览
3

我有一个Spring Boot REST应用程序,分为Resource server和Auth服务器 - 受无状态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 中的这一行链接到auth服务器:

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

auth服务器在数据库中存储使用凭据,并具有以下配置:

@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

但是,我无法弄清楚,如何处理注销(一旦用户决定注销就使令牌无效) . 我假设会提供一些 endpoints 来使令牌无效,或者我是否必须创建自己的 endpoints 来处理它?我不需要指定任何类型的TokenStore bean,因此我不确定如何使当前令牌无效 . 我很高兴有任何见解 - 我发现的大多数教程都解释了如何使用会话或JWT令牌处理这些内容 .

1 回答

  • 1

    我有这个问题,并在this post上发布了一个解决方案 .

    从客户端应用程序注销后,它基本上会重定向到授权服务器上的 endpoints ,然后以编程方式注销 .

相关问题