Browse Source

修正token校验

master
xyiege 4 years ago
parent
commit
8882679a67
  1. 37
      src/main/java/cn/chjyj/szwh/Interceptor/ChInterceptor.java
  2. 5
      src/main/java/cn/chjyj/szwh/controller/admin/AdminGoodsController.java
  3. 2
      src/main/java/cn/chjyj/szwh/controller/api/TestController.java
  4. 3
      src/main/resources/application.properties
  5. 8
      src/main/resources/mapper/szwh/GoodsMapper.xml

37
src/main/java/cn/chjyj/szwh/Interceptor/ChInterceptor.java

@ -9,7 +9,10 @@ import cn.chjyj.szwh.service.impl.AdminServiceImpl;
import cn.chjyj.szwh.utils.JwtUtils; import cn.chjyj.szwh.utils.JwtUtils;
import com.auth0.jwt.JWT; import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT; import com.auth0.jwt.interfaces.DecodedJWT;
import jdk.internal.instrumentation.Logger;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod; import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerInterceptor;
@ -21,8 +24,7 @@ import java.lang.reflect.Method;
@Component @Component
public class ChInterceptor implements HandlerInterceptor { public class ChInterceptor implements HandlerInterceptor {
private static Log log = LogFactory.getLog(ChInterceptor.class);
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 从请求头中获取token // 从请求头中获取token
@ -44,30 +46,33 @@ public class ChInterceptor implements HandlerInterceptor {
else { else {
// 执行认证 // 执行认证
if (token == null) { if (token == null) {
throw new ChException("请登录重试"); throw new ChException("token为空,token为必须参数");
} }else{
}
//log.info("token:"+token);
DecodedJWT decode = JWT.decode(token); DecodedJWT decode = JWT.decode(token);
// todo 1 验证是否过期 // todo 1 验证是否过期
//2 验证是否 //2 验证是否
String userId = decode.getClaim("userid").asString(); String payload=decode.getPayload();
request.setAttribute("pstr",payload);
// String userId = decode.getClaim("userid").asString();
// //获取载荷内容 // //获取载荷内容
// String userName = JwtUtils.getClaimByName(token, "userName").asString(); // String userName = JwtUtils.getClaimByName(token, "userName").asString();
// String realName = JwtUtils.getClaimByName(token, "realName").asString(); // String realName = JwtUtils.getClaimByName(token, "realName").asString();
//找找看是否有这个user 因为我们需要检查用户是否存在,读者可以自行修改逻辑 //找找看是否有这个user 因为我们需要检查用户是否存在,读者可以自行修改逻辑
AdminService adminService = new AdminServiceImpl(); // AdminService adminService = new AdminServiceImpl();
Admin admin = adminService.getAdminByAccountId(userId); // Admin admin = adminService.getAdminByAccountId(userId);
//
if (admin == null) { // if (admin == null) {
//这个错误也是我自定义的 // throw new ChException("用户不存在");
throw new ChException("用户不存在"); // }else{
}else{ // //放入attribute以便后面调用
//放入attribute以便后面调用 // request.setAttribute("uid", admin.getId());
request.setAttribute("uid", admin.getId()); // request.setAttribute("acount_name",admin.getAccountName()); //登录账号
request.setAttribute("acount_name",admin.getAccountName()); //登录账号 // }
}
return true; return true;
} }
return true; return true;

5
src/main/java/cn/chjyj/szwh/controller/admin/AdminGoodsController.java

@ -1,5 +1,6 @@
package cn.chjyj.szwh.controller.admin; package cn.chjyj.szwh.controller.admin;
import cn.chjyj.szwh.annotation.PassToken;
import cn.chjyj.szwh.bean.Goods; import cn.chjyj.szwh.bean.Goods;
import cn.chjyj.szwh.service.GoodsService; import cn.chjyj.szwh.service.GoodsService;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
@ -8,12 +9,13 @@ import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List; import java.util.List;
@Controller @Controller
@RequestMapping(value = "/admin/Goods",method = {RequestMethod.GET,RequestMethod.POST}) @RequestMapping("/admin/admin.Goods")
public class AdminGoodsController { public class AdminGoodsController {
@Autowired @Autowired
private GoodsService goodsService; private GoodsService goodsService;
@ -22,6 +24,7 @@ public class AdminGoodsController {
* 商品列表 * 商品列表
* @return * @return
*/ */
@ResponseBody
@RequestMapping("/list") @RequestMapping("/list")
public String list(@RequestParam(name = "page",defaultValue = "1") String page){ public String list(@RequestParam(name = "page",defaultValue = "1") String page){
int ipage=Integer.parseInt(page); int ipage=Integer.parseInt(page);

2
src/main/java/cn/chjyj/szwh/controller/api/TestController.java

@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.RestController;
*/ */
@RestController @RestController
@RequestMapping("/api/v1/") @RequestMapping("/api/v1")
public class TestController { public class TestController {
/** /**
* test * test

3
src/main/resources/application.properties

@ -6,4 +6,5 @@ logging.path=./logs/
server.port=9090 server.port=9090
# 服务请求上下文 # 服务请求上下文
#server.servlet.context-path=/chapi #server.servlet.context-path=/chapi
mybatis.mapper-locations=classpath*:/mapper/**/*.xml mybatis.mapper-locations=classpath*:/mapper/**/*.xml
spring.

8
src/main/resources/mapper/szwh/GoodsMapper.xml

@ -21,10 +21,10 @@
<!--查询记录列表--> <!--查询记录列表-->
<select id="getGoodsList" parameterType="java.lang.Integer" resultType="cn.chjyj.szwh.bean.Goods"> <select id="getGoodsList" parameterType="java.lang.Integer" resultType="cn.chjyj.szwh.bean.Goods">
WITH mm as (select * from WITH mm as (
<include refid="tbName"/> select <include refid="column"/> from <include refid="tbName"/> where id>#{startRs} limit #{pageSize}
where id>#{startRs} limit #{pageSize} )
)SELECT * FROM mm ORDER BY id DESC; SELECT * FROM mm ORDER BY id DESC;
</select> </select>
<!--根据uid查询用户--> <!--根据uid查询用户-->

Loading…
Cancel
Save