Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用SaCheckRole注解鉴权的时候,对于数组角色标识,只能生效一个,我贴一下我的配置 #608

Open
TenXLL opened this issue Apr 14, 2024 · 0 comments

Comments

@TenXLL
Copy link

TenXLL commented Apr 14, 2024

package com.crane.framework.satoken;

import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import java.util.List;

import com.crane.framework.utils.SystemUtil;
import org.springframework.stereotype.Component;

/** 注入权限组 */
@component
public class StpInterfaceImpl implements StpInterface {

/**

  • 用户权限
  • @param loginId 登录id
  • @param s
  • @return
    */
    @OverRide
    public List getPermissionList(Object loginId, String s) {
    Object authList = StpUtil.getSession().get("authList");
    return SystemUtil.object2List(authList, String.class);
    }

/**

  • 用户角色
  • @param loginId 登录id
  • @param s
  • @return
    */
    @OverRide
    public List getRoleList(Object loginId, String s) {
    Object roleList = StpUtil.getSession().get("roleList");
    return SystemUtil.object2List(roleList, String.class);
    }
    }

package com.crane.framework.satoken;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@configuration
public class SaTokenConfigure implements WebMvcConfigurer {

private static final List excludePath =
List.of(
"/api/v1.0/user/login", "/doc.html", "/favicon.ico", "/webjars/", "/v3/api-docs/");

// 注册拦截器
@OverRide
public void addInterceptors(InterceptorRegistry registry) {
// 注册 Sa-Token 拦截器,校验规则为 StpUtil.checkLogin() 登录校验。
registry
.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
.addPathPatterns("/**")
.excludePathPatterns(excludePath);
}
}

@SaCheckRole(value = {"admin", "root"})
@operation(summary = "获取用户列表")
@PostMapping("/pageUserList")
public Page pageUserList(@requestbody UserPageDTO userPageDTO) {
return userService.getUserList(userPageDTO);
}

private SaTokenInfo login(User user, Object id, List roleList) {
// 登录获取token信息
StpUtil.login(id);
SaTokenInfo tokenInfo = StpUtil.getTokenInfo();

// 获取角色信息
List<String> roleStringList = new ArrayList<>();
if (CommonUtil.notNullOrEmpty(roleList)) {
  roleStringList = roleList.stream().map(Role::getRoleSign).collect(Collectors.toList());
}

// 注入sa-token
StpUtil.getSession().set("roleList", roleStringList);
StpUtil.getSession().set("tenantId", user.getTenantId());
StpUtil.getSession().set("loginName", user.getUsername());

return tokenInfo;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant