码迷,mamicode.com
首页 > 其他好文 > 详细

Interceptor

时间:2019-07-24 00:36:07      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:ken   autowire   request   string   其它   override   int   sys   username   

此处以JwtInterceptor  为例,不需要其它配置


@Component
public class JwtInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    private Jwtutil jwtutil;
    @Autowired
    private SysUserService sysUserService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String header = request.getHeader("Authorization");
        if (!StringUtils.isEmpty(header)) {
            Claims claims = jwtutil.parsetToken(header);
            String username = (String) claims.get("username");
            String role = (String) claims.get("role");
            Integer sysUserId = Integer.valueOf(claims.getId());

            SysUser sysUser = sysUserService.selectByPrimaryKey(sysUserId);
            if (sysUser==null) throw new UserNotLoginException();
            request.setAttribute("sysUser", sysUser);
            request.setAttribute("sysUserId", sysUserId);
            request.setAttribute("username", username);
            request.setAttribute("role", role);
        }
        String url = request.getRequestURL().toString();
        System.out.println(url);
        /**
         *
         * 此处所有请求放行
         */
        return true;
    }
}

 

 

Interceptor

标签:ken   autowire   request   string   其它   override   int   sys   username   

原文地址:https://www.cnblogs.com/alittlesmile/p/11235181.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!