基于SpringBoot实现用户身份验证工具
session失效时间
在Tomcat上,session的默认有效时间是30分钟。也可以通过配置文件修改session的有效时间。
1)修改web.xml
1
2).yml文件
server.session.cookie.http-only=#是否开启HttpOnly server.session.timeout=#会话超时(秒)
使用过滤器获取session进行身份验证(未全部测试,慎用)
1)新建Filter
importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.boot.web.servlet.ServletComponentScan; importorg.springframework.context.ApplicationContext; importorg.springframework.stereotype.Component; importorg.springframework.web.context.support.WebApplicationContextUtils; importjavax.servlet.*; importjavax.servlet.annotation.WebFilter; importjavax.servlet.http.HttpServletRequest; importjava.io.IOException; @Component @ServletComponentScan//让@WebFilter起作用 @WebFilter(urlPatterns="/*") publicclassMyFilterimplementsFilter{ @Autowired privateSessionKeyConfigPropertiessessionKeyConfigProperties; @Override publicvoidinit(FilterConfigfilterConfig)throwsServletException{ } @Override publicvoiddoFilter(ServletRequestservletRequest,ServletResponseservletResponse,FilterChainfilterChain) throwsIOException,ServletException{ HttpServletRequesthttpServletRequest=(HttpServletRequest)servletRequest; System.out.println(sessionKeyConfigProperties.getUserTypeKey()); //通过session获取身份信息 AuthenticationUtilauthenticationUtil=newAuthenticationUtil(sessionKeyConfigProperties); UserTypeEnumuserType=authenticationUtil.getUserAuthentication(httpServletRequest.getSession()); //进行认证 //认证失败 if(userType==null){ //... } //用户不是管理员 if(userType!=UserTypeEnum.ADMIN){ //... } filterChain.doFilter(servletRequest,servletResponse); } @Override publicvoiddestroy(){ } }
细心的读者会发现我用了AuthenticationUtil,这是为了将读写用户身份认证信息的功能分离而设计的工具类 2)AuthenticationUtil类
importorg.apache.shiro.web.session.HttpServletSession; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpSession; publicclassAuthenticationUtil{ privateSessionKeyConfigPropertiesconfigProperties; publicAuthenticationUtil(SessionKeyConfigPropertiesconfigProperties){ this.configProperties=configProperties; } /** *从session中获取用户的身份类型 *@paramsession *@return身份类型 */ publicUserTypeEnumgetUserAuthentication(HttpSessionsession){ //获取session中的用户信息记录 ObjectuserType=session.getAttribute(configProperties.getUserTypeKey()); //获取session中记录的用户类型 if(userType!=null&&userTypeinstanceofUserTypeEnum){ return(UserTypeEnum)userType; } returnnull; } /** *将用户的身份写入session中 *@paramsession *@paramuserType */ publicvoidsetUserAuthentication(HttpSessionsession,UserTypeEnumuserType){ session.setAttribute(configProperties.getUserTypeKey(),userType); } }
3)配置文件SessiionKeyConfig.properties
user_type_key=userTypeKey
4)配置读取文件SessionKeyConfigProperties.class
importorg.springframework.beans.factory.annotation.Value; importorg.springframework.context.annotation.Configuration; importorg.springframework.context.annotation.PropertySource; importorg.springframework.stereotype.Component; @Configuration @PropertySource("classpath:config/SessiionKeyConfig.properties") @Component publicclassSessionKeyConfigProperties{ @Value("${user_type_key}") privateStringuserTypeKey; publicStringgetUserTypeKey(){ returnuserTypeKey; } publicvoidsetUserTypeKey(StringuserTypeKey){ this.userTypeKey=userTypeKey; } }
5)Enum类
publicenumUserTypeEnum{ ADMIN, USER }
注:本文删除了一些package信息及部分import信息。Enum类和配置类的内容请根据项目需求及数据字典自行修改。
总结
以上所述是小编给大家介绍的基于SpringBoot实现用户身份验证工具,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。