国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

基于SpringBoot實現用戶身份驗證工具

2024-07-14 08:39:48
字體:
來源:轉載
供稿:網友

session失效時間

 在Tomcat上,session的默認有效時間是30分鐘。也可以通過配置文件修改session的有效時間。

 1)修改web.xml

<!-- 設置session失效,單位分 --> <session-config>   <session-timeout>1</session-timeout> </session-config>

2).yml文件

server.session.cookie.http-only= #是否開啟HttpOnly server.session.timeout = #會話超時(秒) 

使用過濾器獲取session進行身份驗證(未全部測試,慎用)

1)新建Filter

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; import org.springframework.web.context.support.WebApplicationContextUtils; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import java.io.IOException; @Component @ServletComponentScan//讓@WebFilter起作用 @WebFilter(urlPatterns = "/*") public class MyFilter implements Filter{   @Autowired   private SessionKeyConfigProperties sessionKeyConfigProperties;   @Override   public void init(FilterConfig filterConfig) throws ServletException {   }   @Override   public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)       throws IOException, ServletException {     HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;     System.out.println(sessionKeyConfigProperties.getUserTypeKey());     //通過session獲取身份信息     AuthenticationUtil authenticationUtil = new AuthenticationUtil(sessionKeyConfigProperties);     UserTypeEnum userType = authenticationUtil.getUserAuthentication(httpServletRequest.getSession());     //進行認證     //認證失敗     if(userType == null){       //...     }     //用戶不是管理員     if(userType != UserTypeEnum.ADMIN){       //...     }     filterChain.doFilter(servletRequest,servletResponse);   }   @Override   public void destroy() {   } } 

細心的讀者會發現我用了AuthenticationUtil,這是為了將讀寫用戶身份認證信息的功能分離而設計的工具類  2)AuthenticationUtil類

import org.apache.shiro.web.session.HttpServletSession; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; public class AuthenticationUtil {   private SessionKeyConfigProperties configProperties;   public AuthenticationUtil(SessionKeyConfigProperties configProperties) {     this.configProperties = configProperties;   }   /**    * 從session中獲取用戶的身份類型    * @param session    * @return 身份類型    */   public UserTypeEnum getUserAuthentication(HttpSession session){     //獲取session中的用戶信息記錄     Object userType = session.getAttribute(configProperties.getUserTypeKey());     //獲取session中記錄的用戶類型     if(userType != null && userType instanceof UserTypeEnum) {       return (UserTypeEnum)userType;     }     return null;   }   /**    * 將用戶的身份寫入session中    * @param session    * @param userType    */   public void setUserAuthentication(HttpSession session,UserTypeEnum userType){     session.setAttribute(configProperties.getUserTypeKey(),userType);   } } 

3)配置文件SessiionKeyConfig.properties

user_type_key = userTypeKey 

4)配置讀取文件SessionKeyConfigProperties.class

import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Configuration @PropertySource("classpath:config/SessiionKeyConfig.properties") @Component public class SessionKeyConfigProperties {   @Value("${user_type_key}")   private String userTypeKey;   public String getUserTypeKey() {     return userTypeKey;   }   public void setUserTypeKey(String userTypeKey) {     this.userTypeKey = userTypeKey;   } } 

5)Enum類

public enum UserTypeEnum {   ADMIN,   USER } 

注:本文刪除了一些package信息及部分import信息。Enum類和配置類的內容請根據項目需求及數據字典自行修改。

總結

以上所述是小編給大家介紹的基于SpringBoot實現用戶身份驗證工具,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西林县| 屏东县| 汤原县| 八宿县| 遵化市| 崇明县| 南靖县| 潞城市| 晋江市| 高邑县| 分宜县| 邹平县| 湖南省| 大新县| 保山市| 聂拉木县| 承德县| 二连浩特市| 定兴县| 阿荣旗| 沙湾县| 南澳县| 乌拉特前旗| 泾阳县| 徐闻县| 屏山县| 云梦县| 临夏县| 廉江市| 雅江县| 紫阳县| 定州市| 东乌珠穆沁旗| 剑川县| 南涧| 沙湾县| 济源市| 上栗县| 乌兰县| 个旧市| 嵊州市|