亲宝软件园·资讯

展开

Spring Security数据库认证 Spring Security使用数据库认证及用户密码加密和解密功能

szhwwjava 人气:0

流程图:

1.接上一篇博客https://mp.csdn.net/console/editor/html/104576494,准备好环境。

2.spring-security.xml中的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:security="http://www.springframework.org/schema/security"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans     
  http://www.springframework.org/schema/beans/spring-beans.xsd     
  http://www.springframework.org/schema/security     
  http://www.springframework.org/schema/security/spring-security.xsd">
  
  <!-- 配置不拦截的资源 -->
  <security:http pattern="/login.jsp" security="none"/>
  <security:http pattern="/failer.jsp" security="none"/>
  <security:http pattern="/css/**" security="none"/>
  <security:http pattern="/img/**" security="none"/>
  <security:http pattern="/plugins/**" security="none"/>
  
  <!-- 
    配置具体的规则 
    auto-config="true"  不用自己编写登录的页面,框架提供默认登录页面
    use-expressions="false"  是否使用SPEL表达式(没学习过)
  -->
  <security:http auto-config="true" use-expressions="false">
    <!-- 配置具体的拦截的规则 pattern="请求路径的规则" access="访问系统的人,必须有ROLE_USER或者ROLE_ADMIN的角色" -->
    <security:intercept-url pattern="/**" access="ROLE_USER,ROLE_ADMIN"/>
    
    <!-- 定义跳转的具体的页面 -->
    <security:form-login 
      login-page="/login.jsp"
      login-processing-url="/login.do"//请求路径
      default-target-url="/index.jsp"
      authentication-failure-url="/failer.jsp"
      authentication-success-forward-url="/pages/main.jsp"
    />
    
    <!-- 关闭跨域请求 -->
    <security:csrf disabled="true"/>
    
    <!-- 只要访问到/logout.do就退出,自动跳转到/login.jsp页面 -->
    <security:logout invalidate-session="true" logout-url="/logout.do" logout-success-url="/login.jsp" />
    
  </security:http>
  
  <!-- 切换成数据库中的用户名和密码 -->
  <security:authentication-manager>
    <security:authentication-provider user-service-ref="userService">//userService是认证器需要定义出来
       <!-- 配置加密的方式,用户登录的时候可以知道 -->
      <security:password-encoder ref="passwordEncoder"/>
    </security:authentication-provider>
  </security:authentication-manager>
  
  <!-- 配置加密类,当添加用户的时候,对用户密码进行加密 -->
  <bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
  
  <!-- 提供了入门的方式,在内存中存入用户名和密码 
  <security:authentication-manager>
    <security:authentication-provider>
      <security:user-service>
        <security:user name="admin" password="{noop}admin" authorities="ROLE_USER"/>
      </security:user-service>
    </security:authentication-provider>
  </security:authentication-manager>
  -->
  
 </beans>  

 3.定义一个IUserService继承UserDetailsService接口:

在创建一个UserServiceImpl去实现IUserService接口,覆盖loadUserByUsername方法:

 @Service("userService")//这个名字必须与spring-security.xml中配置的认证器名字一样

public class UserServiceImpl implements IUserService {
  @Autowired
  private IUserDao userDao;
  @Autowired//当执行保存用户的时候对用户的密码进行加密
  private BCryptPasswordEncoder bCryptPasswordEncoder;
  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    UserInfo userInfo = null;
    try {
      userInfo = userDao.findByUsername(username);//调用到层根据用户查找用户信息,返回值为UserInfo对象
    } catch (Exception e) {
      e.printStackTrace();
    }
    //处理自己的用户对象封装成UserDetails
    // User user=new User(userInfo.getUsername(),"{noop}"+userInfo.getPassword(),getAuthority(userInfo.getRoles()));//下面这个三元表达式代表该账户是否激活可用
    User user = new User(userInfo.getUsername(), userInfo.getPassword(), userInfo.getStatus() == 0 ? false : true, true, true, true, getAuthority(userInfo.getRoles()));
    return user;
  }
  //作用就是返回一个List集合,集合中装入的是角色描述
  public List<SimpleGrantedAuthority> getAuthority(List<Role> roles) {
    List<SimpleGrantedAuthority> list = new ArrayList<>();
    for (Role role : roles) {
      list.add(new SimpleGrantedAuthority("ROLE_" + role.getRoleName()));
    }
    return list;
  }
  /**
   * 用户的添加
   * @param userInfo
   */
  @Override
  public void save(UserInfo userInfo) throws Exception {
    //对密码进行加密处理
    userInfo.setPassword(bCryptPasswordEncoder.encode(userInfo.getPassword()));
    userDao.save(userInfo);
  }
}

  4.启动项目进行测试添加用户,新添加的用户是否可以登录成功。

总结

加载全部内容

相关教程
猜你喜欢
用户评论