Interceptor

Posted alittlesmile

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Interceptor相关的知识,希望对你有一定的参考价值。

此处以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的主要内容,如果未能解决你的问题,请参考以下文章