功能说明

  • 配置简单(初始化一个类即可)
  • 满足认证授权基本需求
  • 更加贴近日常使用业务场景
  • 可选本地session和共享session
  • 可选是否支持多端同时登录
  • dubbo、springboot跨服务登录状态传递支持

    使用说明

添加依赖

<dependency>
    <groupId>com.jeesuite</groupId>
    <artifactId>jeesuite-security</artifactId>
    <version>[最新版本]</version>
</dependency>

如何集成

没有复杂配置,实现抽象类SecurityDecisionProvider即可,例如:

@Component
public class OnePlatformSecurityDecisionProvider extends SecurityDecisionProvider {

    private @Autowired ResourceEntityMapper resourceMapper;
    private @Autowired ModuleEntityMapper moduleMapper;
    private @Autowired ResourcesService resourcesService;
    private @Autowired AccountService accountService;

    @Override
    public String contextPath() {
        return ResourceUtils.getProperty("server.servlet.context-path", "");
    }

    @Override
    public String[] anonymousUris() {
        return StringUtils.splitByWholeSeparator(ResourceUtils.getProperty("anonymous.uris"), ";");
    }

    @Override
    public BaseUserInfo validateUser(String name, String password) throws UserNotFoundException, UserPasswordWrongException {
        AccountEntity entity = accountService.findByLoginAccount(name);
        if(entity == null)throw new UserNotFoundException();
        password = AccountEntity.encryptPassword(password);
        if(!password.equals(entity.getPassword()))throw new UserPasswordWrongException();

        LoginUserInfo userInfo = new LoginUserInfo();
        userInfo.setId(entity.getId());
        userInfo.setUserName(entity.getUsername());

        return userInfo;
    }

    @Override
    public List<String> findAllUriPermissionCodes() {
        List<String> result = new ArrayList<>();
        Map<Integer,ModuleEntity> modulesMap = moduleMapper.findAll().stream().collect(Collectors.toMap(ModuleEntity::getId, entity -> entity));
        List<ResourceEntity> resources = resourceMapper.findResources(ResourceType.uri.name());
        ModuleEntity module;
        for (ResourceEntity resource : resources) {
            module = modulesMap.get(resource.getModuleId());
            if(module == null)continue;
            if(GlobalContants.MODULE_NAME.equalsIgnoreCase(module.getServiceId()) 
                    || ModuleType.plugin.name().equals(module.getModuleType())){
                result.add(resource.getResource());
            }else{ 
                result.add("/" + module.getRouteName() + resource.getResource());
            }
        }
        return result;
    }


    @Override
    public List<String> getUserPermissionCodes(Serializable userId) {
        Set<String> codes = resourcesService.findAllPermsByUserId((int)userId);
        return new ArrayList<String>(codes);
    }

    @Override
    public void authorizedPostHandle(UserSession session) {
        LoginSession loginSession = new LoginSession(session.getSessionId(),(int)session.getUserId() , session.getUserName());
        LoginContext.setLoginSession(loginSession);
        RequestContext.getCurrentContext().addZuulRequestHeader(WebConstants.HEADER_AUTH_USER,
                loginSession.toEncodeString());
    }

}

results matching ""

    No results matching ""