【问题标题】:Logout in a thymeleaf client when we use oauth2当我们使用 oauth2 时在 thymeleaf 客户端中注销
【发布时间】:2019-10-14 03:55:41
【问题描述】:

我有 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(token一定要去掉?)

【问题讨论】:

  • 应该是其余的API。安全性旨在保护您的 API 提供的资源。

标签: spring-boot spring-security thymeleaf resttemplate spring-oauth2


【解决方案1】:

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

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

【讨论】:

猜你喜欢
  • 2018-10-25
  • 2018-10-26
  • 2018-12-27
  • 1970-01-01
  • 1970-01-01
  • 2019-11-18
  • 2019-06-10
  • 1970-01-01
  • 2020-08-06
相关资源
最近更新 更多