spring Security的自定义用户认证
首先我需要在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;
}
}
spring Security的自定义用户认证的更多相关文章
- Spring Boot+Spring Security:获取用户信息和session并发控制
说明 (1)JDK版本:1.8(2)Spring Boot 2.0.6(3)Spring Security 5.0.9(4)Spring Data JPA 2.0.11.RELEASE(5)hiber ...
- Spring Security 解析(三) —— 个性化认证 以及 RememberMe 实现
Spring Security 解析(三) -- 个性化认证 以及 RememberMe 实现 在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把 ...
- Spring security 获取当前用户
spring security中当前用户信息 1:如果在jsp页面中获取可以使用spring security的标签库 在页面中引入标签 1 <%@ taglib prefix=" ...
- spring security实现记录用户登录时间等信息
目录 spring security实现记录用户登录时间等信息 一.原理分析 二.实现方式 2.1 自定义AuthenticationSuccessHandler实现类 2.2 在spring-sec ...
- 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_05-SpringSecurityOauth2研究-搭建认证服务器
3 Spring Security Oauth2研究 3.1 目标 本项目认证服务基于Spring Security Oauth2进行构建,并在其基础上作了一些扩展,采用JWT令牌机制,并自定 义了用 ...
- Spring Security默认的用户登录表单 页面源代码
Spring Security默认的用户登录表单 页面源代码 <html><head><title>Login Page</title></hea ...
- Spring Security 之Http Basic认证
使用Spring Security进行http Basic认证非常简单,直接配置即可使用,如下: <security:http> <security:http-basic>&l ...
- 使用django实现自定义用户认证
参考资料:https://docs.djangoproject.com/en/1.10/topics/auth/customizing/ 直接拉到最后看栗子啦 django自定义用户认证(使用自 ...
- 登陆模块,这个是很重要的模块,有shiro和spring security专门的权限认证框架
登陆模块,这个是很重要的模块,有shiro和spring security专门的权限认证框架
随机推荐
- python的mysql数据库操作
python操作mysql数据库 Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数据库 ...
- LeetCode 394. 字符串解码(Decode String) 44
394. 字符串解码 394. Decode String 题目描述 给定一个经过编码的字符串,返回它解码后的字符串. 编码规则为: k[encoded_string],表示其中方括号内部的 enco ...
- yzoj 2372 小B的数字 题解
题意 判断是否存在一个序列 $ b_i $ 使得 $ \prod_{i = 1}^{n} b_i | b_i^{a_i}$ 恒成立,其中 $ b_i $ 中的每个数都是2的正整数次幂. 样例输入 3 ...
- layui 上传图片 实现过程
layui.user一个页面只能有一个,写多了会实现js效果 上传图片官方文档有很多功能,但是演示的代码只是一个一个功能演示,如果要综合起来js代码不是简单的拼凑,需要放在指定位置,比如下面的限制文件 ...
- 关于ElasticSearch的堆内存设置与优化
1.什么是堆内存?Java 中的堆是 JVM 所管理的最大的一块内存空间,主要用于存放各种类的实例对象.在 Java 中,堆被划分成两个不同的区域:- 新生代 ( Young ).- 老年代 ( Ol ...
- WebService 与WebAPI的差异性
对于 WebService和 Web API这两个概念, WebService是一个广义的概念,既 包括采用 RPC的 SOAP WebService,也包括直接建立在 Web 上的非 SOAP We ...
- 七牛云图床存储+Alfread工作流+使用QSHELL
layout: post title: 七牛云图床存储+Alfread工作流+使用QSHELL 来源:http://www.cnblogs.com/cmi-sh-love/p/8901620.html ...
- 如何让 height:100%; 起作用---父级元素必须设定高度
参考: http://www.webhek.com/post/css-100-percent-height.html https://www.cnblogs.com/kunmomo/p/1060066 ...
- spring boot 分布式锁组件 spring-boot-klock-starter
基于redis的分布式锁spring-boot starter组件,使得项目拥有分布式锁能力变得异常简单,支持spring boot,和spirng mvc等spring相关项目 快速开始 sprin ...
- CGContextRef&CGMutablePathRef&UIBezierPath简单学习
简单的四句介绍 Quartz是一个二维绘图引擎,使用的是CoreGraphics库,同时支持iOS和Mac系统 CGContextRef:获取图形上下文.或者叫作用域,即画布,他是专门用来保存绘画期间 ...