springcloud整合satoken

satoken简介:

https://sa-token.cc/doc.html#/

注入的依赖

其他服务注入的依赖

	        
     <dependency>
            <groupId>com.linmain.commongroupId>
            <artifactId>common-coreartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>

        
        
        
        
        

        
        <dependency>
            <groupId>cn.dev33groupId>
            <artifactId>sa-token-dao-redis-jacksonartifactId>
        dependency>

        
        <dependency>
            <groupId>cn.dev33groupId>
            <artifactId>sa-token-spring-boot-starterartifactId>
        dependency>

        
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-pool2artifactId>
        dependency>

网关注入的依赖

        
        <dependency>
            <groupId>cn.dev33groupId>
            <artifactId>sa-token-dao-redis-jacksonartifactId>
        dependency>

        
        <dependency>
            <groupId>cn.dev33groupId>
            <artifactId>sa-token-reactor-spring-boot-starterartifactId>
        dependency>

        
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-pool2artifactId>
        dependency>

实现权限接口和注解鉴权开启

放到common-core中的

package com.linmain.satoken.config;

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

/**
 * @Author linzhuoqi
 * @Date 2023/2/19
 * @Eamil [email protected]
 * @Version
 * @Description
 *Sa-Token 使用全局拦截器完成注解鉴权功能,为了不为项目带来不必要的性能负担,拦截器默认处于关闭状态
 * 因此,为了使用注解鉴权,你必须手动将 Sa-Token 的全局拦截器注册到你项目中
 */
public class SaTokenConfigure implements WebMvcConfigurer {

    /**
     * 注册 Sa-Token 拦截器,打开注解式鉴权功能
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册 Sa-Token 拦截器,打开注解式鉴权功能
        registry.addInterceptor(new SaInterceptor()).addPathPatterns("/**");
    }
}
package com.linmain.satoken.config;

import cn.dev33.satoken.stp.StpInterface;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author linzhuoqi
 * @Date 2023/2/18
 * @Eamil [email protected]
 * @Version
 * @Description   自定义权限验证接口扩展
 */
public class StpInterfaceImpl implements StpInterface {

    /**
     * 添加权限
     * @param loginId
     * @param loginType
     * @return
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        return null;
    }

    /**
     * 添加角色
     * @param loginId
     * @param loginType
     * @return
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        String userAccount = loginId.toString();
        List<String> list = new ArrayList<>();

        //TODO 规定管理员的账号长度为12位
        if (userAccount.length() == 12) {
            list.add("admin");
        } else {
            list.add("app");
        }
        return list;
    }
}

权限服务控制层

package com.linmain.auth.controller.api;


import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.linmain.auth.service.UserAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 前台用户(UserApp)表控制层
 *
 * @Author linzhuoqi
 * @Date 2023-02-17 00:07:24
 * @Eamil [email protected]
 * @Version
 * @Description   
 */
@RestController
@RequestMapping("/api/auth")
@Api(tags = " 前台用户认证接口")
public class UserAppController{


    /**
     * 服务对象
     */
    @Resource
    private UserAppService userAppService;
    @ApiOperation(value = "前台用户登录")
    @PostMapping("/doLogin")
    public SaResult doLogin(String userAccount, String password) {
        if (userAppService.doLogin(userAccount, password)) {
            StpUtil.login(userAccount);
            // 第2步,获取 Token  相关参数
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            return SaResult.data(tokenInfo);
        }
        return SaResult.error("登录失败");
    }

    @ApiOperation(value = "注销")
    @DeleteMapping("/logout")
    public SaResult logout() {
        StpUtil.logout();
        return SaResult.ok();
    }

    @ApiOperation(value = "查询登录状态")
    @PostMapping("/isLogin")
    public SaResult isLogin() {
        return SaResult.ok("是否登录:" + StpUtil.isLogin());
    }


    @ApiOperation(value ="查询 Token 信息")
    @PostMapping("/tokenInfo")
    public SaResult tokenInfo() {
        return SaResult.data(StpUtil.getTokenInfo());
    }
}

你可能感兴趣的:(spring,cloud,java,spring)