SpringSecurity整合JWT(四)-創(chuàng)新互聯(lián)

一、前言

本篇文章將講述Spring Security 簡(jiǎn)單整合JWT 處理認(rèn)證授權(quán)

撫松網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),撫松網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為撫松近1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的撫松做網(wǎng)站的公司定做!基本環(huán)境
  1. spring-boot 2.1.8
  2. mybatis-plus 2.2.0
  3. mysql 數(shù)據(jù)庫(kù)
  4. maven項(xiàng)目
Spring Security入門(mén)學(xué)習(xí)可參考之前文章:
  1. SpringBoot集成Spring Security入門(mén)體驗(yàn)(一)
    https://blog.csdn.net/qq_38225558/article/details/101754743
  2. Spring Security 自定義登錄認(rèn)證(二)
    https://blog.csdn.net/qq_38225558/article/details/102542072
  3. Spring Security 動(dòng)態(tài)url權(quán)限控制(三)
    https://blog.csdn.net/qq_38225558/article/details/102637637

二、 Spring Security 簡(jiǎn)單整合 JWT

有關(guān)JWT不了解的可以看下官網(wǎng)文檔:https://jwt.io/introduction/
Spring Security 整合JWT(四)

1、引入jwt依賴(lài)
 <!-- jwt依賴(lài): https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt -->
 <dependency>
     <groupId>io.jsonwebtoken</groupId>
     <artifactId>jjwt</artifactId>
     <version>0.9.1</version>
 </dependency>
2、在Security登錄認(rèn)證成功后生成jwt令牌返回給前端保存

jwt生成令牌代碼如下:

// 生成jwt訪(fǎng)問(wèn)令牌
String jwtToken = Jwts.builder()
        // 用戶(hù)角色
        .claim("ROLE_LOGIN", "ADMIN")
        // 主題 - 存用戶(hù)名
        .setSubject("張三")
        // 過(guò)期時(shí)間 - 30分鐘
        .setExpiration(new Date(System.currentTimeMillis() + 30 * 60 * 1000))
        // 加密算法和密鑰
        .signWith(SignatureAlgorithm.HS512, "helloworld")
        .compact();

這里貼出小編文末案例demo源碼中關(guān)于登錄認(rèn)證處理中的使用

@Component
public class AdminAuthenticationProvider implements AuthenticationProvider {

    @Autowired
    UserDetailsServiceImpl userDetailsService;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        // 獲取前端表單中輸入后返回的用戶(hù)名、密碼
        String userName = (String) authentication.getPrincipal();
        String password = (String) authentication.getCredentials();

        SecurityUser userInfo = (SecurityUser) userDetailsService.loadUserByUsername(userName);

        boolean isValid = PasswordUtils.isValidPassword(password, userInfo.getPassword(), userInfo.getCurrentUserInfo().getSalt());
        // 驗(yàn)證密碼
        if (!isValid) {
            throw new BadCredentialsException("密碼錯(cuò)誤!");
        }

        // 前后端分離情況下 處理邏輯...
        // 更新登錄令牌
        // 當(dāng)前用戶(hù)所擁有角色代碼
        String roleCodes = userInfo.getRoleCodes();
        // 生成jwt訪(fǎng)問(wèn)令牌
        String jwt = Jwts.builder()
                // 用戶(hù)角色
                .claim(Constants.ROLE_LOGIN, roleCodes)
                // 主題 - 存用戶(hù)名
                .setSubject(authentication.getName())
                // 過(guò)期時(shí)間 - 30分鐘
                .setExpiration(new Date(System.currentTimeMillis() + 30 * 60 * 1000))
                // 加密算法和密鑰
                .signWith(SignatureAlgorithm.HS512, Constants.SALT)
                .compact();

        User user = userMapper.selectById(userInfo.getCurrentUserInfo().getId());
        user.setToken(jwt);
        userMapper.updateById(user);
        userInfo.getCurrentUserInfo().setToken(jwt);
        return new UsernamePasswordAuthenticationToken(userInfo, password, userInfo.getAuthorities());
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return true;
    }
}

前端頁(yè)面保存的jwt令牌格式如下:
Spring Security 整合JWT(四)

3、Security訪(fǎng)問(wèn)鑒權(quán)中認(rèn)證用戶(hù)信息

我們?cè)谠L(fǎng)問(wèn)每一個(gè)url請(qǐng)求的時(shí)候,在統(tǒng)一認(rèn)證的地方獲取jwt中我們需要的信息然后認(rèn)證即可,【注: Claims 中存放著我們需要的信息】
例如: 我們可以將用戶(hù)名、密碼存放jwt中,然后在認(rèn)證的時(shí)候讀取到其中的用戶(hù)信息,然后查詢(xún)數(shù)據(jù)庫(kù)認(rèn)證用戶(hù),如果滿(mǎn)足條件即成功訪(fǎng)問(wèn),如果不滿(mǎn)足條件即拋出異常處理

溫馨小提示:如果jwt令牌過(guò)期,會(huì)拋出ExpiredJwtException異常,我們需要攔截到,然后交給認(rèn)證失敗處理器中處理,然后返回給前端,這里根據(jù)個(gè)人業(yè)務(wù)實(shí)際處理即可~

// 獲取jwt中的信息
Claims claims = Jwts.parser().setSigningKey("helloworld").parseClaimsJws(jwtToken.replace("Bearer", "")).getBody();
// 獲取當(dāng)前登錄用戶(hù)名
System.out.println("獲取當(dāng)前登錄用戶(hù)名: " + claims.getSubject());

小編項(xiàng)目中認(rèn)證過(guò)濾器中的使用如下:

@Slf4j
@Component
public class MyAuthenticationFilter extends OncePerRequestFilter {

    @Autowired
    AdminAuthenticationEntryPoint authenticationEntryPoint;

    private final UserDetailsServiceImpl userDetailsService;

    protected MyAuthenticationFilter(UserDetailsServiceImpl userDetailsService) {
        this.userDetailsService = userDetailsService;
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        MultiReadHttpServletRequest wrappedRequest = new MultiReadHttpServletRequest(request);
        MultiReadHttpServletResponse wrappedResponse = new MultiReadHttpServletResponse(response);
        StopWatch stopWatch = new StopWatch();
        try {
            stopWatch.start();
            // 前后端分離情況下,前端登錄后將token儲(chǔ)存在cookie中,每次訪(fǎng)問(wèn)接口時(shí)通過(guò)token去拿用戶(hù)權(quán)限
            String jwtToken = wrappedRequest.getHeader(Constants.REQUEST_HEADER);
            log.debug("后臺(tái)檢查令牌:{}", jwtToken);
            if (StringUtils.isNotBlank(jwtToken)) {
                // JWT相關(guān)start ===========================================
                // 獲取jwt中的信息
                Claims claims = Jwts.parser().setSigningKey(Constants.SALT).parseClaimsJws(jwtToken.replace("Bearer", "")).getBody();
                // 獲取當(dāng)前登錄用戶(hù)名
                System.out.println("獲取當(dāng)前登錄用戶(hù)名: " + claims.getSubject());
                // TODO 如需使用jwt特性在此做處理~
                // JWT相關(guān)end ===========================================

                // 檢查token
                SecurityUser securityUser = userDetailsService.getUserByToken(jwtToken);
                if (securityUser == null || securityUser.getCurrentUserInfo() == null) {
                    throw new BadCredentialsException("TOKEN已過(guò)期,請(qǐng)重新登錄!");
                }
                UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(securityUser, null, securityUser.getAuthorities());
                // 全局注入角色權(quán)限信息和登錄用戶(hù)基本信息
                SecurityContextHolder.getContext().setAuthentication(authentication);
            }
            filterChain.doFilter(wrappedRequest, wrappedResponse);
        } catch (ExpiredJwtException e) {
            // jwt令牌過(guò)期
            SecurityContextHolder.clearContext();
            this.authenticationEntryPoint.commence(wrappedRequest, response, null);
        } catch (AuthenticationException e) {
            SecurityContextHolder.clearContext();
            this.authenticationEntryPoint.commence(wrappedRequest, response, e);
        } finally {
            stopWatch.stop();
        }
    }

}

簡(jiǎn)單的入門(mén)使用就是這樣了

三、總結(jié)

  1. 引入jwt依賴(lài)
  2. 登錄系統(tǒng)成功后生成jwt令牌返回給前端保存到瀏覽器請(qǐng)求頭
  3. 在每一次請(qǐng)求訪(fǎng)問(wèn)系統(tǒng)url時(shí),在統(tǒng)一認(rèn)證過(guò)濾器中獲取到請(qǐng)求頭中jwt令牌中保存的用戶(hù)信息然后做認(rèn)證處理,如果滿(mǎn)足條件成功訪(fǎng)問(wèn),如果不滿(mǎn)足交給認(rèn)證失敗處理器返回指定內(nèi)容給前端
本文案例demo源碼

https://gitee.com/zhengqingya/java-workspace

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。

名稱(chēng)欄目:SpringSecurity整合JWT(四)-創(chuàng)新互聯(lián)
分享鏈接:http://bm7419.com/article16/dseigg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開(kāi)發(fā)、App設(shè)計(jì)、移動(dòng)網(wǎng)站建設(shè)、Google、虛擬主機(jī)網(wǎng)站設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開(kāi)發(fā)