我想建立图书管理的管理门户(获取,添加,删除,更新一本书)。因此,我从登录/注销功能开始;登录部分工作起来很有魅力,但我面临着注销的问题。当我注销时,admin门户会保持会话的活力,并且不会杀死它,并且我无法在日志中获得任何错误来证明这个问题。
下面是一段响应post注销请求的代码:
@RequestMapping(value = "/user/logout", method = RequestMethod.POST)
public ResponseEntity logout(HttpServletRequest request, HttpServletResponse response) {
SecurityContextHolder.clearContext();
return new ResponseEntity("Logout Successful !", HttpStatus.OK);
}在客户机中,我使用Angular将post请求发送给本地server.Below (注销服务):
logout(){
let url="http://localhost:8090/user/logout";
const xToken = localStorage.getItem('xAuthToken');
let headers=new Headers({
'x-auth-token':xToken,
});
return this.http.post(url,{headers:headers});
}xToken变量从本地浏览器存储中获取会话的令牌。
每次我们在登录组件的ngOnInit中检查会话是否处于活动状态时:
ngOnInit() {
this.login.checkSession().subscribe(
res=>{
this.loggedIn=true;
},
err=>{
console.log(err);
this.loggedIn=false;
}
)
}负责会话检查的服务详细描述如下:
checkSession(){
let url="http://localhost:8090/checkSession";
const xToken = localStorage.getItem('xAuthToken');
const basicHeader = 'Basic ' + localStorage.getItem('credentials');
let headers=new Headers({
'x-auth-token':xToken,
'Authorization':basicHeader
});
return this.http.get(url,{headers:headers});
}为了检查会话,我们请求服务器:
@RequestMapping("/checkSession")
public ResponseEntity checkSession() {
System.out.print("Session Active !");
return new ResponseEntity("Session Active !", HttpStatus.OK);
}basicHeader常量是一种身份验证方案,包含存储在浏览器本地存储中的编码用户名/密码组合。
有关我们如何向服务器发送凭据的下面的其他信息:
onSubmit(){
this.login.sendCredentials(this.credentials.username,this.credentials.password).subscribe(
res=>{
console.log(res);
localStorage.setItem('xAuthToken', res.json().token);
this.loggedIn=true;
const encodedCredentials = btoa(this.credentials.username + ':' + this.credentials.password);
localStorage.setItem("credentials",encodedCredentials);
location.reload();
},err=>{
console.log(err);
}
)
}非常感谢您的任何帮助。谢谢你提前解决了这个问题
发布于 2018-10-12 20:58:25
Security支持logout特性,它只需要一些配置:
@EnableWebSecurity
@Configuration
public class SecurityConf extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) {
http
.httpBasic().and() // HTTP Basic authorization or whichever
.authorizeRequests()
.antMatchers(...).permitAll()
.anyRequest().authenticated().and()
...
.logout();
}
...
}然后,假设您的应用程序是通过/logout端口在localhost上启动的,则http://localhost:8080/logout通过POST方法访问该方法。
有关更多细节,弹簧保安医生
https://stackoverflow.com/questions/52784756
复制相似问题