资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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

这篇文章主要介绍基于SpringBoot怎么实现用户身份验证工具,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

成都创新互联公司是一家专业提供景德镇企业网站建设,专注与网站设计、成都做网站、H5页面制作、小程序制作等业务。10年已为景德镇众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。

session失效时间

 在Tomcat上,session的默认有效时间是30分钟。也可以通过配置文件修改session的有效时间。

 1)修改web.xml

 
 
  1 

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是什么

springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。

以上是“基于SpringBoot怎么实现用户身份验证工具”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!


网页题目:基于SpringBoot怎么实现用户身份验证工具
本文地址:http://cdkjz.cn/article/ghieoj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220