日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

添加拦截器解析用户信息

發布時間:2024/4/13 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 添加拦截器解析用户信息 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

已登錄購物車

接下來,我們完成已登錄購物車。

在剛才的未登錄購物車編寫時,我們已經預留好了編寫代碼的位置,邏輯也基本一致。

添加登錄校驗

購物車系統只負責登錄狀態的購物車處理,因此需要添加登錄校驗,我們通過JWT鑒權即可實現。

引入JWT相關依賴

我們引入之前寫的鑒權工具:learn-auth-common

<dependency><groupId>com.learn.auth</groupId><artifactId>learn-auth-common</artifactId><version>1.0.0-SNAPSHOT</version> </dependency>

配置公鑰

learn:jwt:pubKeyPath: C:/tmp/rsa/rsa.pub # 公鑰地址cookieName: LY_TOKEN # cookie的名稱

加載公鑰

代碼:

@ConfigurationProperties(prefix = "learn.jwt") public class JwtProperties {private String pubKeyPath;// 公鑰private PublicKey publicKey; // 公鑰private String cookieName;private static final Logger logger = LoggerFactory.getLogger(JwtProperties.class);@PostConstructpublic void init(){try {// 獲取公鑰和私鑰this.publicKey = RsaUtils.getPublicKey(pubKeyPath);} catch (Exception e) {logger.error("初始化公鑰失敗!", e);throw new RuntimeException();}}public String getPubKeyPath() {return pubKeyPath;}public void setPubKeyPath(String pubKeyPath) {this.pubKeyPath = pubKeyPath;}public PublicKey getPublicKey() {return publicKey;}public void setPublicKey(PublicKey publicKey) {this.publicKey = publicKey;}public String getCookieName() {return cookieName;}public void setCookieName(String cookieName) {this.cookieName = cookieName;} }

編寫攔截器

因為很多接口都需要進行登錄,我們直接編寫SpringMVC攔截器,進行統一登錄校驗。同時,我們還要把解析得到的用戶信息保存起來,以便后續的接口可以使用。

代碼:

public class LoginInterceptor extends HandlerInterceptorAdapter {private JwtProperties jwtProperties;// 定義一個線程域,存放登錄用戶private static final ThreadLocal<UserInfo> tl = new ThreadLocal<>();public LoginInterceptor(JwtProperties jwtProperties) {this.jwtProperties = jwtProperties;}@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {// 查詢tokenString token = CookieUtils.getCookieValue(request, "LY_TOKEN");if (StringUtils.isBlank(token)) {// 未登錄,返回401response.setStatus(HttpStatus.UNAUTHORIZED.value());return false;}// 有token,查詢用戶信息try {// 解析成功,證明已經登錄UserInfo user = JwtUtils.getInfoFromToken(token, jwtProperties.getPublicKey());// 放入線程域tl.set(user);return true;} catch (Exception e){// 拋出異常,證明未登錄,返回401response.setStatus(HttpStatus.UNAUTHORIZED.value());return false;}}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {tl.remove();}public static UserInfo getLoginUser() {return tl.get();} }

注意:

  • 這里我們使用了ThreadLocal來存儲查詢到的用戶信息,線程內共享,因此請求到達Controller后可以共享User

  • 并且對外提供了靜態的方法:getLoginUser()來獲取User信息

配置攔截器

配置SpringMVC,使過濾器生效:

@Configuration @EnableConfigurationProperties(JwtProperties.class) public class MvcConfig implements WebMvcConfigurer {@Autowiredprivate JwtProperties jwtProperties;@Beanpublic LoginInterceptor loginInterceptor() {return new LoginInterceptor(jwtProperties);}@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(loginInterceptor()).addPathPatterns("/**");} }

?

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的添加拦截器解析用户信息的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。