Spring boot 使用oauth2时在thymeleaf客户端中注销
我有3个应用程序,一个用于授权,一个带有资源(api rest),一个在thymeleaf中使用其余资源的客户端 当我从我的客户端注销时,这似乎不是真正的注销,因为当我单击login时,它会直接登录我。。。使用以前的用户 我以贝尔东为例,他们和我的相似,也有同样的问题 授权服务器 资源服务器 胸腺嘧啶核苷客户端 在授权服务器中,我有Spring boot 使用oauth2时在thymeleaf客户端中注销,spring-boot,spring-security,thymeleaf,resttemplate,spring-oauth2,Spring Boot,Spring Security,Thymeleaf,Resttemplate,Spring Oauth2,我有3个应用程序,一个用于授权,一个带有资源(api rest),一个在thymeleaf中使用其余资源的客户端 当我从我的客户端注销时,这似乎不是真正的注销,因为当我单击login时,它会直接登录我。。。使用以前的用户 我以贝尔东为例,他们和我的相似,也有同样的问题 授权服务器 资源服务器 胸腺嘧啶核苷客户端 在授权服务器中,我有 @Controller public class TokenController { @Resource(name = "tokenServices
@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;
}
}
@控制器
公共类令牌控制器{
@资源(name=“令牌服务”)
私人消费服务代币服务;
@资源(name=“tokenStore”)
私人代币店;
@RequestMapping(method=RequestMethod.POST,value=“/oauth/token/revokeById/{tokenId}”)
@应答器
public void revokeToken(HttpServletRequest请求,@PathVariable字符串令牌ID){
tokenServices.revokeToken(tokenId);
}
@RequestMapping(method=RequestMethod.GET,value=“/tokens”)
@应答器
公共列表getTokens(){
Collection tokens=tokenStore.findTokensByClientId(“sampleClientId”);
返回可选的.ofNullable(tokens).orElse(Collections.emptyList()).stream().map(OAuth2AccessToken::getValue).collection(Collections.toList());
}
@RequestMapping(method=RequestMethod.POST,value=“/tokens/revokefreshttoken/{tokenId:.*}”)
@应答器
公共字符串revokeRefreshToken(@PathVariable String tokenId){
if(JdbcTokenStore的tokenStore实例){
((JdbcTokenStore)tokenStore.RemoveFreshToken(tokenId);
}
返回tokenId;
}
}
在我的thymeleaf客户端中
@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;
}
}
@EnableWebSecurity
公共类SecurityConfig扩展了WebSecurity配置适配器{
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
http.authorizeRequests()
.antMatchers(“/”).permitAll()
.anyRequest().authenticated()
.及()
.oauth2Login()
.及()
.logout().logoutSuccessUrl(“/”);
}
@豆子
公共RestTemplate RestTemplate(OAuth2AuthorizedClientService客户端服务){
RestTemplate RestTemplate=新RestTemplate();
List interceptors=restemplate.getInterceptors();
if(CollectionUtils.isEmpty(拦截器)){
拦截器=新的ArrayList();
}
add(新授权HeaderInterceptor(clientService));
restTemplate.setInterceptors(拦截器);
返回REST模板;
}
}
如何真正从thymeleaf注销(必须删除令牌?应用程序的每一层都需要“安全性”。但我猜你的帖子指的是认证层
如果您不希望RESTAPI可供所有人使用,那么应该向其添加一个身份验证层,至少添加一个CORS策略。想想API密钥、OAuth或普通的旧用户名/密码身份验证。应该是rest API。安全性是指保护API提供的资源的安全性