在我的Spring会话中,我使用了 redis 服务器。现在我正在使用 Angular 7。
这里代码1
logout()
{
this.loginService.logout().subscribe(
res =>
{
location.reload();
},
err =>
{
console.log(err);
}
)
}
2
import { MyAccountComponent } from './../components/my-account/my-account.component';
import { AppConst } from './../constants/app-const';
import { Router } from '@angular/router';
import { Injectable } from '@angular/core';
import { HttpClient,HttpHeaders } from '@angular/common/http';
@Injectable()
export class LoginService {
private serverPath:string =AppConst.serverPath;
constructor(private http: HttpClient, private router:Router) { }
sendCredential(username: string, password: string) {
let url = this.serverPath+'/token';
let encodedCredentials = btoa(username+":"+password);
let basicHeader = "Basic "+encodedCredentials;
let headers = new HttpHeaders ({
'content-Type' : 'application/x-www-form-urlencoded',
'Authorization' : basicHeader
});
return this.http.get(url,{headers:headers});
}
checkSession(){
let url = this.serverPath+'/user/checkSession';
let headers = new HttpHeaders ({
'x-auth-token' : localStorage.getItem('xAuthToken')
});
console.log("Value of token at check Session ",localStorage.getItem('xAuthToken'));
return this.http.get(url,{responseType:'text'});
}
logout(){
let url = this.serverPath+'/user/logoutUser';
let headers = new HttpHeaders ({
'x-auth-token' : localStorage.getItem('xAuthToken')
});
return this.http.post(url,'',{responseType:'text'});
// return this.http.post(url,'',{headers:headers});
}
ngOnInit() {
//Called after the constructor, initializing input properties, and the first call to ngOnChanges.
//Add 'implements OnInit' to the class.
}
}
3
package com.bookstore.resource;
import java.util.Collections;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class LoginResource {
@RequestMapping("/token")
public Map<String,String> token(HttpSession session,HttpServletRequest req){
System.out.println(req.getRemoteHost());
String remoteHost=req.getRemoteHost();
int port =req.getRemotePort();
System.out.println("This is remoteHost:Port Number"+remoteHost+":"+port);
System.out.println("This is GetRemoteAddr"+req.getRemoteAddr());
return Collections.singletonMap("token",session.getId());
}
@RequestMapping("/user/checkSession")
public ResponseEntity checkSession()
{
System.out.print(HttpStatus.OK);
return new ResponseEntity("Session Activated!",HttpStatus.OK);
}
@RequestMapping(value="/user/logoutUser",method=RequestMethod.POST)
public ResponseEntity logout()
{
SecurityContextHolder.clearContext();
System.out.println("louout");
return new ResponseEntity("Logout done",HttpStatus.OK);
}
}
4
package com.bookstore.config;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Order(Ordered.HIGHEST_PRECEDENCE)
@Component
public class RequestFilter implements Filter{
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with, x-auth-token");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Credentials", "true");
System.out.println("value of request "+request.getMethod());
if(!(request.getMethod().equalsIgnoreCase("OPTIONS"))) {
System.out.println("At if");
try {
chain.doFilter(req, res);
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("Pre-fight");
response.setHeader("Access-Control-Allowed-Methods", "POST, GET, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "authorization, content-type, x-auth-token, " +
"access-control-request-headers,access-control-request-method,accept,origin,authorization,x-requested-with");
response.setStatus(HttpServletResponse.SC_OK);
}
}
public void init(FilterConfig filterConfig) {}
public void destroy() {}
}
>
点击注销按钮后,我的页面将重新加载,但注意happen.it将与上一页相同。
如果我把{headers:headers}放在返回的loginservice中。logout(),则会出现错误
TypeError: Cannot read property 'length' of null
at http.js:109
at Array.forEach (<anonymous>)
at HttpHeaders.lazyInit (http.js:103)
at HttpHeaders.push../node_modules/@angular/common/fesm5/http.js.HttpHeaders.init
(http.js:167),位于HttpHeaders.push../node_modules/@angular/common/fesm5/http.js.HttpHeaders。Observable上的forEach(http.js:236)_在Observable.push../node_modules/rxjs/_esm5/internal/Observaable.js.Observarable订阅(http.js:1436)_trySubscribe(Observable.js:43)位于Observarable.push../node_modules/rxjs/_esm5/internal/Observaable.js.Observatable。在subscribeTo上订阅(Observable.js:29)。js:21在subscriptToResult(subscripttoResult.js:11)
您已经得到了注销的代码:
SecurityContextHolder.clearContext();
我本以为会是这样的:
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null){
new SecurityContextLogoutHandler().logout(request, response, auth);
}
需要明确的是,这不是路由问题或权限问题吗?您确实看到服务器端确实访问了“/user/logoutUser”请求映射?
它给出: 下面是我使用的代码: 方法verifyUserExistance是; 我就是这样处理注销的;
下面是我用raspberry PI的python(Thonny Idle)编写的代码。 请忽略Url,它不是真实地址。密码 错误 回溯(最近一次呼叫最后一次): 文件“/home/pi/Documents/PythonCode/TestingFirebase-1.py”,第17行,在 文件“/usr/local/lib/python3.7/dist-packages/firebase/decora
我正在阅读“ProSpring3”一书,并尝试在我的项目中使用Spring Data JPA功能。我在这里发布相关文件。 src/main/java/foo/bar/domain/ContactAudit。java语言: src/main/java/foo/bar/repository/ContactAuditRepository.java : src/main/java/foo/bar/serv
为了获得基本的安全功能,我在我的pom.xml中添加了以下初学者包 并在应用程序中添加了以下两个属性。属性: 安全使用者name=客人 安全。使用者密码=tiger 现在,当我点击我的主页时,我得到了登录框,登录工作正常。 现在我想实现“注销”功能。当用户单击链接时,他/她就会注销。我注意到登录没有在我的浏览器中添加任何cookie。我假设Spring Security为用户创建了一个HttpSe
我是角7的新手。我想构建Spring Boot+Angular 7应用程序,但我不知道Spring Boot+Angular 7是如何一起工作的。为了解释我的问题,我创建了一个示例项目,在其中定义了rest控制器和angular应用程序,在其中创建了模块和定义了路由。现在,当我从IDE内部运行Spring boot应用程序时,我无法理解应用程序的行为。产生的一些问题是: 1)当我启动应用程序并在浏