欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

spring Security的自定义用户认证过程详解

时间:2021-02-24 09:37:56|栏目:JAVA代码|点击:

首先我需要在xml文件中声明.我要进行自定义用户的认证类,也就是我要自己从数据库中进行查询

<http pattern="/*.html" security="none"/>
  <http pattern="/css/**" security="none"/>
  <http pattern="/img/**" security="none"/>
  <http pattern="/js/**" security="none"/>
  <http pattern="/plugins/**" security="none"/>
  <http pattern="/seller/add.do" security="none"/>

  <!-- use-expressions:设置是否启动SpEL表达式,默认值是true。 -->
  <http use-expressions="false">
    <!--
      配置SpringSecurity的拦截路径(拦截规则)
      * pattern:配置拦截规则。  /* 代表的是根路径下的所有资源(不包含子路径) /**代表的是根路径下所有的资源(包含子路径)
      * access:设置角色 角色命名 ROLE_角色名称 如: ROLE_USER
    -->
    <intercept-url pattern="/**" access="ROLE_SELLER"/>

    <!--
    开启表单验证
      username-parameter="username"
      password-parameter="password"
      login-page      :登录页面名称 以 / 开始
      default-target-url  :登录成功后跳转的页面
      login-processing-url:提交的路径的设置 默认值"/login" 可以修改
    -->
    <form-login login-page="/shoplogin.html" default-target-url="/admin/index.html" always-use-default-target="true" authentication-failure-url="/shoplogin.html"/>

    <!-- 不使用csrf的校验 -->
    <csrf disabled="true"/>

    <!-- 配置框架页面不拦截 -->
    <headers>
      <frame-options policy="SAMEORIGIN"/>
    </headers>

    <!-- 注销的配置 -->
    <logout logout-url="/logout" logout-success-url="/shoplogin.html" />
  </http>

  <!-- 配置认证管理器 -->
  <authentication-manager>
    <!-- 认证的提供者 -->
    <authentication-provider user-service-ref="userDetailService">
      <password-encoder ref="passwordEncoder"></password-encoder>
    </authentication-provider>
  </authentication-manager>
<!-- 配置自定义的认证类 -->
  <beans:bean id="userDetailService" class="com.qingmu2.core.service.UserDetailServiceImpl">
    <beans:property name="sellerService" ref="sellerService"></beans:property>
  </beans:bean>

<!--加密时候使用的算法是BCryptPasswordEncoder-->
  <beans:bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>

配置完自定义的文件以后,在需要自定义认证类的模块中实现

UserDetailsService

package com.qingmu2.core.service;
import com.alibaba.dubbo.config.annotation.Reference;
import com.qingmu2.core.pojo.seller.Seller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/**
 * 自定义的认证类
 * @Auther:qingmu
 * @Description:脚踏实地,只为出人头地
 * @Date:Created in 8:33 2019/5/31
 */
public class UserDetailServiceImpl implements UserDetailsService {

  private SellerService sellerService;

  public UserDetailServiceImpl() {
  }
  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    Seller seller = sellerService.findOne(username);
    if(null!=seller){
      //判断一次商家是否被审核通过.
      if("1".equals(seller.getStatus())){
        //创建一个集合,用来存储权限
        HashSet<GrantedAuthority> authorities = new HashSet<>();
        authorities.add(new SimpleGrantedAuthority("ROLE_SELLER"));
        //将这个用户的信息返回给认证类
        return new User(username,seller.getPassword(),authorities);
      }
    }
    //没有这个用户,则返回null
    return null;
  }

  public UserDetailServiceImpl(SellerService sellerService) {
    this.sellerService = sellerService;
  }

  public SellerService getSellerService() {
    return sellerService;
  }

  public void setSellerService(SellerService sellerService) {
    this.sellerService = sellerService;
  }
}

上一篇:idea 打包的jar运行报 "XXX中没有主清单属性"

栏    目:JAVA代码

下一篇:SpringMVC源码解析之消息转换器HttpMessageConverter

本文标题:spring Security的自定义用户认证过程详解

本文地址:http://www.codeinn.net/misctech/68995.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有