前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >权限管理与Shiro入门(六)

权限管理与Shiro入门(六)

作者头像
用户1289394
发布2023-10-03 13:26:34
1130
发布2023-10-03 13:26:34
举报
文章被收录于专栏:Java学习网Java学习网

3.3 拦截器中鉴权

(1)在ihrm-common下添加拦截器 JwtInterceptor

代码语言:javascript
复制
@Component
public class JwtInterceptor extends HandlerInterceptorAdapter {
    @Autowired    
    private JwtUtil jwtUtil;    
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, 
Object handler) throws Exception {
        // 1.通过request获取请求token信息
        String authorization = request.getHeader("Authorization");
        //判断请求头信息是否为空,或者是否已Bearer开头
        if(!StringUtils.isEmpty(authorization) && authorization.startsWith("Bearer")) {
            //获取token数据
            String token = authorization.replace("Bearer ","");
            //解析token获取claims
            Claims claims = jwtUtils.parseJwt(token);
            if(claims != null) {
                //通过claims获取到当前用户的可访问API权限字符串
                String apis = (String) claims.get("apis");  //api-user-delete,api-userupdate 
                      //通过handler
                HandlerMethod h = (HandlerMethod) handler;
                //获取接口上的reqeustmapping注解
                RequestMapping annotation =
h.getMethodAnnotation(RequestMapping.class);
                //获取当前请求接口中的name属性
                String name = annotation.name();
                //判断当前用户是否具有响应的请求权限
                if(apis.contains(name)) {
                    request.setAttribute("user_claims",claims);
                    return true;
               }else {
                    throw new CommonException(ResultCode.UNAUTHORISE);
               }
           }
       }
        throw new CommonException(ResultCode.UNAUTHENTICATED);
   }
}

(2)修改UserController的profile方法

代码语言:javascript
复制
/**
     * 用户登录成功之后,获取用户信息
     *     1.获取用户id
     *     2.根据用户id查询用户
     *     3.构建返回值对象
     *     4.响应
     */
    @RequestMapping(value="/profile",method = RequestMethod.POST)
    public Result profile(HttpServletRequest request) throws Exception {
        String userid = claims.getId();
        //获取用户信息
        User user = userService.findById(userid);
        //根据不同的用户级别获取用户权限
        ProfileResult result = null;
        if("user".equals(user.getLevel())) {
            result = new ProfileResult(user);
       }else {
            Map map = new HashMap();
            if("coAdmin".equals(user.getLevel())) {
                map.put("enVisible","1");
           }
            List<Permission> list = permissionService.findAll(map);
            result = new ProfileResult(user,list);
       }
        return new Result(ResultCode.SUCCESS,result);
   }

(3)配置拦截器类,创建com.ihrm.system.SystemConfig

代码语言:javascript
复制
package com.ihrm.system;
import com.ihrm.common.interceptor.JwtInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class SystemConfig extends WebMvcConfigurationSupport {
    @Autowired
    private JwtInterceptor jwtInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtInterceptor).
        addPathPatterns("/**").
        excludePathPatterns("/frame/login","/frame/register/**"); //设置不拦截的请求地址
   }
}
本文参与?腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-10-03 07:30,如有侵权请联系?cloudcommunity@tencent.com 删除

本文分享自 Java学习网 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com