首页 > 解决方案 > 当我们使用 oauth2 时在 thymeleaf 客户端中注销

问题描述

我有 3 个应用程序,一个用于授权,一个具有资源(api rest)和一个在 thymeleaf 中消耗其余部分的客户端。

当我注销客户端时,这似乎不是真正的注销,因为当我单击登录时,直接登录我...使用以前的用户。

我以 Baeldung 为例,它与我的相似并且有同样的问题。

授权服务器 https://github.com/Baeldung/spring-security-oauth/tree/master/oauth-authorization-server

资源服务器 https://github.com/Baeldung/spring-security-oauth/tree/master/oauth-resource-server-1

Thymeleaf 客户端 https://github.com/Baeldung/spring-security-oauth/tree/master/clients-thymeleaf/oauth-ui-authorization-code-thymeleaf

在我的授权服务器中

@Controller
public class TokenController {

    @Resource(name = "tokenServices")
    private ConsumerTokenServices tokenServices;

    @Resource(name = "tokenStore")
    private TokenStore tokenStore;

    @RequestMapping(method = RequestMethod.POST, value = "/oauth/token/revokeById/{tokenId}")
    @ResponseBody
    public void revokeToken(HttpServletRequest request, @PathVariable String tokenId) {
        tokenServices.revokeToken(tokenId);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/tokens")
    @ResponseBody
    public List<String> getTokens() {
        Collection<OAuth2AccessToken> tokens = tokenStore.findTokensByClientId("sampleClientId");
        return Optional.ofNullable(tokens).orElse(Collections.emptyList()).stream().map(OAuth2AccessToken::getValue).collect(Collectors.toList());
    }

    @RequestMapping(method = RequestMethod.POST, value = "/tokens/revokeRefreshToken/{tokenId:.*}")
    @ResponseBody
    public String revokeRefreshToken(@PathVariable String tokenId) {
        if (tokenStore instanceof JdbcTokenStore) {
            ((JdbcTokenStore) tokenStore).removeRefreshToken(tokenId);
        }
        return tokenId;
    }

}

在百里香客户我有

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .anyRequest().authenticated()
                .and()
                .oauth2Login()
                .and()
                .logout().logoutSuccessUrl("/");
    }

    @Bean
    public RestTemplate restTemplate(OAuth2AuthorizedClientService clientService) {
        RestTemplate restTemplate = new RestTemplate();
        List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
        if (CollectionUtils.isEmpty(interceptors)) {
            interceptors = new ArrayList<>();
        }
        interceptors.add(new AuthorizationHeaderInterceptor(clientService));
        restTemplate.setInterceptors(interceptors);
        return restTemplate;
    }

}

如何真正从 thymeleaf 中注销(必须确定删除令牌?)

标签: spring-bootspring-securitythymeleafresttemplatespring-oauth2

解决方案


应用程序的每一层都需要“安全”。但我猜你的帖子是指身份验证层。

如果您不希望您的 REST API 可供所有人使用,您应该为其添加一个身份验证层,并至少添加一个 CORS 策略。想想 API 密钥、OAuth 或普通的旧用户名/密码身份验证。


推荐阅读