基于SpringBoot实现用户身份验证工具

 更新时间:2018年04月03日 10:22:09   作者:玩具熊猫  
这篇文章主要介绍了基于SpringBoot实现的用户身份验证工具,非常不错,具有参考借鉴价值 ,需要的朋友可以参考下

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实现用户身份验证工具,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • Mybatis千万级数据查询的解决方式,避免OOM问题

    Mybatis千万级数据查询的解决方式,避免OOM问题

    这篇文章主要介绍了Mybatis千万级数据查询的解决方式,避免OOM问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • springboot的jar包如何启用外部配置文件

    springboot的jar包如何启用外部配置文件

    本文主要介绍了springboot的jar包如何启用外部配置文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • 解决Callable的对象中,用@Autowired注入别的对象失败问题

    解决Callable的对象中,用@Autowired注入别的对象失败问题

    这篇文章主要介绍了解决Callable的对象中,用@Autowired注入别的对象失败问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • 详解Maven POM(项目对象模型)

    详解Maven POM(项目对象模型)

    这篇文章主要介绍了Maven POM(项目对象模型)的相关资料,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • java Scanner类的使用示例代码

    java Scanner类的使用示例代码

    这篇文章主要介绍了java Scanner类的使用,Scanner类还可以任意地对字符串和基本类型(如int和double)的数据进行分析。借助于Scanner,可以针对任何要处理的文本内容编写自定义的语法分析器,感兴趣的朋友跟随小编一起看看吧
    2021-07-07
  • 教你使用Java获取当前时间戳的详细代码

    教你使用Java获取当前时间戳的详细代码

    这篇文章主要介绍了如何使用Java获取当前时间戳,通过两个java示例,向大家展示如何获取java中的当前时间戳,文本通过示例代码给大家展示了java获取当前时间戳的方法,需要的朋友可以参考下
    2022-01-01
  • java实现对map的字典序排序操作示例

    java实现对map的字典序排序操作示例

    这篇文章主要介绍了java实现对map的字典序排序操作,结合实例形式分析了java参照微信官网算法实现的字典序排序操作相关实现技巧,需要的朋友可以参考下
    2019-07-07
  • MyBatis Generator生成数据库模型实现示例

    MyBatis Generator生成数据库模型实现示例

    这篇文章主要为大家介绍了MyBatis Generator生成数据库模型实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • 搭建 springboot selenium 网页文件转图片环境的详细教程

    搭建 springboot selenium 网页文件转图片环境的详细教程

    这篇文章主要介绍了搭建 springboot selenium 网页文件转图片环境,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • 详解Java数据库连接JDBC基础知识(操作数据库:增删改查)

    详解Java数据库连接JDBC基础知识(操作数据库:增删改查)

    这篇文章主要介绍了详解Java数据库连接JDBC基础知识(操作数据库:增删改查),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01

最新评论