欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springSecurity自定義登錄接口和JWT認(rèn)證過濾器的流程

 更新時(shí)間:2024年12月12日 10:33:56   作者:機(jī)躍  
這篇文章主要介紹了springSecurity自定義登陸接口和JWT認(rèn)證過濾器的相關(guān)資料,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧

下面我會(huì)根據(jù)該流程圖去自定義接口:

我們需要做的任務(wù)有:

 登陸:1、通過ProviderManager的方法進(jìn)行認(rèn)證,生成jwt;2、把用戶信息存入redis;3、自定義UserDetailsService實(shí)現(xiàn)到數(shù)據(jù)庫(kù)查詢數(shù)據(jù)的方法。

校驗(yàn):自定義一個(gè)jwt認(rèn)證過濾器,其實(shí)現(xiàn)功能:獲取token;解析token;從redis獲取信息;存入SecurityContextHolder。

登陸:

圖中的 5.1步驟是到內(nèi)存中查詢用戶信息,而我們需要的是到數(shù)據(jù)庫(kù)中查詢。而圖中查詢用戶信息是調(diào)用loadUserbyUsername方法實(shí)現(xiàn)的。

所以我們需要實(shí)現(xiàn)UserDetailsService接口并重寫該方法:(下面案例中我用的mybatis plus實(shí)現(xiàn)的查詢數(shù)據(jù)庫(kù))

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;
    //loadUserByUsername方法即為流程圖中查詢用戶信息的方法。
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //查詢用戶信息
        LambdaQueryWrapper<User> queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName,username);
        User user = userMapper.selectOne(queryWrapper);
        //如果沒有查詢到用戶
        if(Objects.isNull(user)){
            throw new RuntimeException("用戶名或密碼錯(cuò)誤");
        }
        //封裝為UserDetails類型返回
        return new LoginUser(user);
    }
}

我們先寫好登陸功能的controller層代碼:

@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;
    @PostMapping("/user/login")
    public ResponseResult login(@RequestBody User user){
        //登陸
        return loginService.login(user);
    }

 我們需要讓springSecurity對(duì)該登陸接口放行,不需要登陸就能訪問。在登陸service層接口中需要通過AuthenticationManager的authenticate方法進(jìn)行用戶認(rèn)證,我們先在SecurityConfig中把AuthenticationManager注入容器。

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    @Bean //(name = "") //獲取AuthenticationManager的bean,因?yàn)楝F(xiàn)在只有這一個(gè)AuthenticationManager,所以不寫也沒事。
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }
    //放開接口
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //關(guān)閉csrf,csrf為跨域策略,不支持post
                .csrf().disable()
                //不通過session獲取SecurityContext 前后端分離時(shí)session不可用
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                //對(duì)于登陸接口,允許匿名訪問,登陸之后不允許訪問,只允許匿名的用戶,可以防止重復(fù)登陸。
                .antMatchers("/user/login").anonymous() //permitAll() 登錄能訪問,不登錄也能訪問,一般用于靜態(tài)資源js等
                //除了上面外,所有請(qǐng)求需要鑒權(quán)認(rèn)證
                .anyRequest().authenticated();//authenticated():任意用戶,認(rèn)證后都可訪問。
    }
}

然后我們?nèi)バ薷牡顷懡涌诘膕ervice層實(shí)現(xiàn)類代理:

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private RedisCache redisCache;
    //登陸
    @Override
    public ResponseResult login(User user) {
        //獲取AuthenticationManager的authenticate方法進(jìn)行認(rèn)證。
        //通過SecurityConfig獲取AuthenticationManager
        //創(chuàng)建Authentication,第一個(gè)參數(shù)為認(rèn)證主體,沒有的話傳用戶名,第二個(gè)參數(shù)傳密碼
        UsernamePasswordAuthenticationToken authenticationToken
                =new UsernamePasswordAuthenticationToken(user.getUserName(),user.getPassword());
        //需要Authentication參數(shù)(上面)
        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
        //這樣讓ProviderManager調(diào)用UserDetailsService類中的loadUserByUsername方法完成認(rèn)證
        //如果認(rèn)證不通過,authenticate為null
        //認(rèn)證沒通過,給出提示
        if(Objects.isNull(authenticate)){
            throw new RuntimeException("登陸失敗");
        }
        //認(rèn)證通過,使用userid生成jwt,jwt存入ResponseResult返回
          //獲取userId
        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
        String userId = loginUser.getUser().getId().toString();
        String jwt = JwtUtil.createJWT(userId);
        Map<String,String> map = new HashMap<>();
        map.put("token",jwt);
        //完整信息存入redis,userid作key
        redisCache.setCacheObject("login:"+userId,loginUser);
        return new ResponseResult(200,"登陸成功",map);
    }
}

springSecurity流程圖中是通過獲取AuthenticationManager的authenticate方法進(jìn)行認(rèn)證。通過SecurityConfig中注入的bean獲取AuthenticationManager。

authenticationManager的authenticate方法需要一個(gè)Authentication實(shí)現(xiàn)類參數(shù),所以我們創(chuàng)建一個(gè)UsernamePasswordAuthenticationToken實(shí)現(xiàn)類

其中的JwtUtil.createJWT(userId);方法,是我自定義的根據(jù)userId生成JWT的工具類方法:

public class JwtUtil {
    //有效期為
    public static final Long JWT_TTL = 60*60*1000L;//一個(gè)小時(shí)
    //設(shè)置密鑰明文 。隨便定義,方便記憶和使用即可,但需要長(zhǎng)度要為4的倍數(shù)。
    public static final String JWT_KEY = "jyue";
    public static String getUUID(){
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        return token;
    }
    //生成JWT
    //subject為token中存放的數(shù)據(jù)(json格式)
    public static String createJWT(String subject){
        JwtBuilder builder = getJwtBuilder(subject, null, getUUID());//設(shè)置過期時(shí)間
        return builder.compact();
    }
    public static JwtBuilder getJwtBuilder(String subject,Long ttlMillis,String uuid){
        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
        SecretKey secretKey=generalKey();
        long nowMillis = System.currentTimeMillis();
        Date now = new Date(nowMillis);
        if(ttlMillis ==null){
            ttlMillis=JwtUtil.JWT_TTL;
        }
        long expMills=nowMillis+ttlMillis;
        Date expDate = new Date(expMills);
        return Jwts.builder()
                .setId(uuid) //唯一id
                .setSubject(subject) //主題 可以是JSON數(shù)據(jù)
                .setIssuer("jy") //簽發(fā)者,隨便寫
                .setIssuedAt(now) //簽發(fā)時(shí)間
                .signWith(signatureAlgorithm,secretKey) //使用HS256對(duì)稱加密算法簽名,第二個(gè)參數(shù)為密鑰。
                .setExpiration(expDate);
    }
    public static SecretKey generalKey(){
        byte[] encodeKey = Base64.getDecoder().decode(JwtUtil.JWT_KEY);
        SecretKeySpec key = new SecretKeySpec(encodeKey, 0, encodeKey.length, "AES");
        return key;
    }
    public static Claims parseJWT(String jwt)throws Exception{
        SecretKey secretKey = generalKey();
        return Jwts.parser()
                .setSigningKey(secretKey)
                .parseClaimsJws(jwt)
                .getBody();
    }
}

redisCache也為自定義的redis工具類:

@SuppressWarnings(value = {"unchecked","rawtypes"})
@Component
public class RedisCache {
    @Autowired
    public RedisTemplate redisTemplate;
    //緩存對(duì)象
    //key 緩存鍵值
    //value 緩存值
    public <T> void setCacheObject(final String key,final T value){
        redisTemplate.opsForValue().set(key,value);
    }
    //獲取緩存的基本對(duì)象
    // key 鍵值
    // return 緩存鍵對(duì)應(yīng)的數(shù)據(jù)
    public <T>T getCacheObject(final String key){
        ValueOperations<String,T> operation = redisTemplate.opsForValue();
        return operation.get(key);
    }
}

JWT認(rèn)證:

@Component            //繼承這個(gè)實(shí)現(xiàn)類,保證了請(qǐng)求只會(huì)經(jīng)過該過濾器一次
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
    @Autowired
    private RedisCache redisCache;
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        //首先需要從請(qǐng)求頭中獲取token
        String token = request.getHeader("token");
        //判斷token是否為Null
        if(!StringUtils.hasText(token)) {
            //token沒有的話,直接放行,拋異常的活交給后續(xù)專門的過濾器。
            filterChain.doFilter(request, response);
            //響應(yīng)時(shí)還會(huì)經(jīng)過該過濾器一次,直接return,不能執(zhí)行下面的解析token的代碼。
            return;
        }
        //如何不為空,解析token,獲得了UserId
        String userId;
        try {
            Claims claims = JwtUtil.parseJWT(token);
            userId = claims.getSubject();
        } catch (Exception e) {
            e.printStackTrace();
            //token格式異常,不是正經(jīng)token
            throw new RuntimeException("token非法");
        }
        //根據(jù)UserId查redis獲取用戶數(shù)據(jù)
        String key = "login:"+userId;
        LoginUser loginUser = redisCache.getCacheObject(key);
        if(Objects.isNull(loginUser)){
            throw new RuntimeException("用戶未登錄");
        }
        //然后封裝Authentication對(duì)象存入SecurityContextHolder
        // 因?yàn)楹罄m(xù)的過濾器會(huì)從SecurityContextHolder中獲取信息判斷認(rèn)證情況,而決定是否放行。
        // 這里用UsernamePasswordAuthenticationToken三個(gè)參數(shù)的構(gòu)造函數(shù),是因?yàn)槠淠茉O(shè)置已認(rèn)證的狀態(tài)(因?yàn)橐呀?jīng)從redis中獲取了信息,確認(rèn)是認(rèn)證的了)
        //第一個(gè)參數(shù)為用戶信息,第三個(gè)參數(shù)為權(quán)限信息,目前還沒獲取,先填null
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser,null,null);
        //默認(rèn)SecurityContextHolder是ThreadLocal線程私有的,這也是為什么上面要用UsernamePasswordAuthenticationToken三個(gè)參數(shù)的構(gòu)造方法
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        filterChain.doFilter(request,response);
    }
}

這樣登陸后用戶發(fā)送請(qǐng)求,后端會(huì)先從請(qǐng)求頭中獲取token,然后解析出userId,然后從redis中查詢?cè)撚脩粼敿?xì)信息。然后把用戶的詳細(xì)信息存入U(xiǎn)sernamePasswordAuthenticationToken三個(gè)參數(shù)的構(gòu)造函數(shù),是因?yàn)槠淠茉O(shè)置已認(rèn)證的狀態(tài)(因?yàn)橐呀?jīng)從redis中獲取了信息,確認(rèn)是認(rèn)證的了),然后把UsernamePasswordAuthenticationToken存入SecurityContextHolder。

因?yàn)楹罄m(xù)的過濾器會(huì)從SecurityContextHolder中獲取信息判斷認(rèn)證情況,而決定是否放行。

到此這篇關(guān)于springSecurity自定義登陸接口和JWT認(rèn)證過濾器的文章就介紹到這了,更多相關(guān)springSecurity自定義登陸接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解Java冒泡排序

    詳解Java冒泡排序

    本篇文章通過代碼實(shí)例給大家詳細(xì)分析了Java冒泡排序的原理,有興趣的朋友可以學(xué)習(xí)下。
    2018-02-02
  • 深入理解hibernate的三種狀態(tài)

    深入理解hibernate的三種狀態(tài)

    本篇文章主要介紹了深入理解hibernate的三種狀態(tài) ,主要包括了transient(瞬時(shí)狀態(tài)),persistent(持久化狀態(tài))以及detached(離線狀態(tài)),有興趣的同學(xué)可以了解一下
    2017-05-05
  • Java實(shí)現(xiàn)AES加密和解密方式完整示例

    Java實(shí)現(xiàn)AES加密和解密方式完整示例

    這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)AES加密和解密方式的相關(guān)資料,AES加密為最常見的對(duì)稱加密算法,是一種區(qū)塊加密標(biāo)準(zhǔn),這個(gè)標(biāo)準(zhǔn)用來替代原先的DES,已經(jīng)被多方分析且廣為全世界所使用,需要的朋友可以參考下
    2023-10-10
  • Java 選擇排序、插入排序、希爾算法實(shí)例詳解

    Java 選擇排序、插入排序、希爾算法實(shí)例詳解

    這篇文章主要介紹了Java 選擇排序、插入排序、希爾算法實(shí)例詳解,需要的朋友可以參考下
    2017-05-05
  • springboot讀取自定義配置文件節(jié)點(diǎn)的方法

    springboot讀取自定義配置文件節(jié)點(diǎn)的方法

    這篇文章主要介紹了springboot讀取自定義配置文件節(jié)點(diǎn)的方法,本文給大家介紹的非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2018-05-05
  • Java多線程基礎(chǔ)

    Java多線程基礎(chǔ)

    這篇文章主要介紹Java多線程基礎(chǔ),線程是進(jìn)程的一個(gè)實(shí)體,是CPU調(diào)度和分派的基本單位,它是比進(jìn)程更小的能獨(dú)立運(yùn)行的基本單位,多線程指在單個(gè)程序中可以同時(shí)運(yùn)行多個(gè)不同的線程執(zhí)行不同的任務(wù),下面來學(xué)習(xí)具體的詳細(xì)內(nèi)容
    2021-10-10
  • Java中import java.util.Scanner的用處詳解

    Java中import java.util.Scanner的用處詳解

    文章主要介紹Java中的Scanner類及其常用方法next()和nextLine()的區(qū)別,next()方法在遇到空格、Tab鍵、回車鍵等分隔符時(shí)結(jié)束輸入,而nextLine()方法則接收所有輸入,直到遇到回車鍵
    2024-11-11
  • Spring?DI依賴注入過程解析

    Spring?DI依賴注入過程解析

    依賴注入是由“依賴”和“注入”兩個(gè)詞匯組合而成,那么我們?cè)僖淮雾樚倜希謩e分析這兩個(gè)詞語(yǔ),這篇文章主要介紹了Spring?DI依賴注入詳解,需要的朋友可以參考下
    2022-11-11
  • SpringBoot整合RabbitMQ消息隊(duì)列的完整步驟

    SpringBoot整合RabbitMQ消息隊(duì)列的完整步驟

    這篇文章主要給大家介紹了關(guān)于SpringBoot整合RabbitMQ消息隊(duì)列的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • dubbo之@Reference注解作用說明

    dubbo之@Reference注解作用說明

    這篇文章主要介紹了dubbo之@Reference注解作用說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03

最新評(píng)論