标签:tchar top 跨域请求 creat equals 拦截器 token 判断 lis
在前后端分离时,实现登陆权限控制。 在这使用token来处理,提前说明后端采用的是SSM框架
后端需要完成的操作如下:
1、生成token的工具类TokenUtil(内含用户名+密码生成token值和token验证是否正确的两个方法):
package com.lw.util;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class TokenUtil {
private static final long EXPIRE_TIME = 15 * 60 * 1000; // 设置过期时间为15分钟
private static final String TOKEN_SECRET = "JNVCOR&$@)_(#VSZ+_?CV}{XNVM^&"; // 设置签名私钥
/**
* 生成token,可以将你认为需要的数据当成参数存入
*/
public static String sign(String userName,String password) {
try {
// 设置过期时间
Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
// 私钥和加密算法
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
// 设置头部信息
Map<String, Object> header = new HashMap<String, Object>(2);
header.put("Type", "Jwt");
header.put("alg", "HS256");
// 返回token字符串
return JWT.create()
.withHeader(header)
.withClaim("userName", userName)
.withClaim("password",password)
.sign(algorithm);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 检验token是否正确语句
*/
public static boolean verify(String token){
try {
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
JWTVerifier verifier = JWT.require(algorithm).build();
DecodedJWT jwt = verifier.verify(token);
return true;
} catch (Exception e){
return false;
}
}
/**
* 获取token中的参数值
* @param token
* @return
*/
public static String getUserName(String token){
try{
DecodedJWT jwt = JWT.decode(token);
return jwt.getClaim("userName").asString();
}catch (Exception e){
return null;
}
}
/**
* 获取token中的参数值
* @param token
* @return
*/
public static String getPassword(String token){
try{
DecodedJWT jwt = JWT.decode(token);
return jwt.getClaim("password").asString();
}catch (Exception e){
return null;
}
}
}
2、导入工具类TokenInterceptor(获取前端token值进行校验是否相等)
package com.lw.controller.interceptor;
import com.alibaba.fastjson.JSONObject;
import com.lw.util.Result;
import com.lw.util.TokenUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TokenInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handle) throws Exception {
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "*");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "*");
response.setHeader("Content-Type", "application/json;charset=utf-8");
String token = request.getHeader("token"); //获取在前端存储的token值。
response.setCharacterEncoding("utf-8");
//跨域ajax请求,都会先发一次method为OPTIONS的预请求
//1、获取服务器支持的HTTP请求方法。
//2、用来检查服务器的性能。例如:AJAX进行跨域请求时的预检,需要向另外一个域名的资源发送一个HTTP OPTIONS请求头,用以判断实际发送的请求是否安全。
if (request.getMethod().equals("OPTIONS")) {
System.out.println("OPTIONS");
response.setStatus(HttpServletResponse.SC_OK);
return true;
}
else if (null != token) {
//token不为空,验证是否token是否正确
if ( TokenUtil.verify(token)) {
return true;
}
}
Result result = Result.err("用户过期,请重新登录");
String json = JSONObject.toJSONString(result);
response.getWriter().write(json);
return false;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
}
3、在Spring-mvc.xml文件中配置token拦截器:(起拦截作用,可设置实现未登录时不能访问该页面权限)
<!--配置token校验拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/topic/comment"/>
<mvc:mapping path="/topic/send"/>
<mvc:mapping path="/topic/like"/>
<mvc:mapping path="/topic/unlike"/>
<mvc:mapping path="/topic/delete"/>
<mvc:mapping path="/home/user/profile"/>
<mvc:mapping path="/home/user/editSubmit"/>
<bean class="com.lw.controller.interceptor.TokenInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
标签:tchar top 跨域请求 creat equals 拦截器 token 判断 lis
原文地址:https://www.cnblogs.com/zxl971213/p/12979994.html