上一篇讲了springSecurity的简单入门的小demo,认证用户是在xml中写死的。今天来说一下自定义认证,读取数据库来实现认证。当然,也是非常简单的,因为仅仅是读取数据库,权限是写死的,因为相对简单,没几个角色,就直接写死了。

还有就是加密,使用的是框架自带的   BCryptPasswordEncoder   加密方法。存在数据库的用户密码也是通过这个类加密,然后登陆的时候也是通过这个类验证,需要在xml中配置下就ok。

简单说一下这个加密类。比md5更加的高级。

加密分为  :

可逆(秘钥)

不可逆(哈希算法)(BCryptPasswordEncoder 和md5 都属于 )

理论上md5是不可逆的,但是其实上是可以破解的,如果不想被破解,可以采用加盐的方法,BCryptPasswordEncoder就是自己生成随机盐,即使密码一样得到的加密后的密码也不一样,这大大增加了破解的难度。

大体步骤:

自己写一个类实现   UserDetailsService  这个接口  ,实现一个方法,最主要是返回一个User对象,这个对象是框架提供的。具体看代码。

然后将这个自定义的类在xml中配置一下,就是说原来写死的用户信息删除掉,采用这个类来验证。 就ok  非常简单

UserDetailsServiceImpl.java    // 认证类
package com.pinyougou.service;

import com.pinyougou.pojo.TbSeller;
import com.pinyougou.sellergoods.service.SellerService;
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.List;
/**
* 认证类
* @author Administrator
*
*/
public class UserDetailsServiceImpl implements UserDetailsService { private SellerService sellerService; public void setSellerService(SellerService sellerService) {
this.sellerService = sellerService;
} @Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
System.out.println("经过了UserDetailsServiceImpl");
//构建角色列表
List<GrantedAuthority> grantAuths=new ArrayList();
grantAuths.add(new SimpleGrantedAuthority("ROLE_SELLER")); //得到商家对象
TbSeller seller = sellerService.findOne(username);
if(seller!=null){
if(seller.getStatus().equals("1")){
return new User(username,seller.getPassword(),grantAuths);
}else{
return null;
}
}else{
return null;
}
} }

spring-security.xml       // 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd"> <!-- 设置页面不登陆也可以访问 -->
<http pattern="/*.html" security="none"></http>
<http pattern="/css/**" security="none"></http>
<http pattern="/img/**" security="none"></http>
<http pattern="/js/**" security="none"></http>
<http pattern="/plugins/**" security="none"></http>
<http pattern="/seller/add.do" security="none"></http> <!-- 页面的拦截规则 use-expressions:是否启动SPEL表达式 默认是true -->
<http use-expressions="false">
<!-- 当前用户必须有ROLE_USER的角色 才可以访问根目录及所属子目录的资源 -->
<intercept-url pattern="/**" access="ROLE_SELLER"/>
<!-- 开启表单登陆功能 -->
<form-login login-page="/shoplogin.html" default-target-url="/admin/index.html" authentication-failure-url="/shoplogin.html" always-use-default-target="true"/>
<csrf disabled="true"/>
<headers>
<frame-options policy="SAMEORIGIN"/>
</headers>
<logout/>
</http> <!-- 认证管理器 -->
<authentication-manager>
<authentication-provider user-service-ref="userDetailService">
<password-encoder ref="bcryptEncoder"></password-encoder>
</authentication-provider>
</authentication-manager> <!-- 自定义认证类 -->
<beans:bean id="userDetailService" class="com.pinyougou.service.UserDetailsServiceImpl">
<beans:property name="sellerService" ref="sellerService"></beans:property>
</beans:bean> <!-- 引用dubbo 服务 因为认证类需要用到这个服务,但是这个服务是在dubbo中的,远程的,所以需要采用这种方法来引入,不能
在向以前那样直接spring注解的方法引入了,切记。-->
<dubbo:application name="pinyougou-shop-web" />
<dubbo:registry address="zookeeper://47.98.157.114:2181"/>
<dubbo:reference id="sellerService" interface="com.pinyougou.sellergoods.service.SellerService"></dubbo:reference> <!--加密类-->
<beans:bean id="bcryptEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"></beans:bean>
</beans:beans>
												

springSecurity自定义认证配置的更多相关文章

  1. SpringSecurity——基于Spring、SpringMVC和MyBatis自定义SpringSecurity权限认证规则

    本文转自:https://www.cnblogs.com/weilu2/p/springsecurity_custom_decision_metadata.html 本文在SpringMVC和MyBa ...

  2. 【Spring Security】六、自定义认证处理的过滤器

    这里接着上一章的自定义过滤器,这里主要的是配置自定义认证处理的过滤器,并加入到FilterChain的过程.在我们自己不在xml做特殊的配置情况下,security默认的做认证处理的过滤器为Usern ...

  3. JavaWeb-SpringSecurity自定义登陆配置

    系列博文 项目已上传至guthub 传送门 JavaWeb-SpringSecurity初认识 传送门 JavaWeb-SpringSecurity在数据库中查询登陆用户 传送门 JavaWeb-Sp ...

  4. SpringSecurity 自定义用户 角色 资源权限控制

    SpringSecurity 自定义用户 角色 资源权限控制 package com.joyen.learning.security; import java.sql.ResultSet; impor ...

  5. SpringSecurity 自定义表单登录

    SpringSecurity 自定义表单登录 本篇主要讲解 在SpringSecurity中 如何 自定义表单登录 , SpringSecurity默认提供了一个表单登录,但是实际项目里肯定无法使用的 ...

  6. SpringSecurity之认证

    SpringSecurity之认证 目录 SpringSecurity之认证 1. 盐值加密 1. 原理概述 2. 使用说明 1. 加密 2. 认证 1. 页面成功跳转的坑 2. 使用验证码校验的坑 ...

  7. SpringSecurity自定义注解和处理器

    登录功能 添加一个配置类 @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Reso ...

  8. ocelot 自定义认证和授权

    ocelot 自定义认证和授权 Intro 最近又重新启动了网关项目,服务越来越多,每个服务都有一个地址,这无论是对于前端还是后端开发调试都是比较麻烦的,前端需要定义很多 baseUrl,而后端需要没 ...

  9. asp.net core 自定义认证方式--请求头认证

    asp.net core 自定义认证方式--请求头认证 Intro 最近开始真正的实践了一些网关的东西,最近写几篇文章分享一下我的实践以及遇到的问题. 本文主要介绍网关后面的服务如何进行认证. 解决思 ...

随机推荐

  1. CSS布局的一些技巧

    max-width 通常使元素水平居中用的较多的方法为: #main { width: 600px; margin: 0 auto; } 但是,当浏览器窗口比元素的宽度还要窄时,浏览器会显示一个水平滚 ...

  2. M2 Daily SCRUM要求

    每个人的工作 (有work item 的ID):昨天已完成的工作,今天计划完成的工作:工作中遇到的困难. 燃尽图 照片 每人的代码/文档签入记录(不能每天都在 “研讨”, 但是没有代码签入) 如实报告 ...

  3. 20135202闫佳歆--week6 分析Linux内核创建一个新进程的过程——实验及总结

    week 6 实验:分析Linux内核创建一个新进程的过程 1.使用gdb跟踪创建新进程的过程 准备工作: rm menu -rf git clone https://github.com/mengn ...

  4. Git学习笔记 --第一章

    本系列学习笔记参考廖雪峰Git教程 安装Git Linux平台 Ubuntu/Debian命令:sudo apt-get install git 其他版本  Git官网下载源码,然后解压,依次输入:. ...

  5. 四则运算APP,团队项目之需求

    队名:IG.Super 成员:范铭祥,曾威,刘恒,黄伟俊. 一.程序功能需求 程序可以出带括号的正整数四则运算,支持分数,除法保留两位小数,如:(1/3+1)*2 = 2.67,特别注意:这里是2.6 ...

  6. 浅谈个人对存储区域网络SAN的理解

    存储区域网络SAN,是一种通过将网络存储设备和服务器连接起来的网络,提供计算机和存储设备间的数据传输.其中,SAN是独立于服务器系统之外的,拥有近乎无限的存储能力,通过利用光纤作为传输媒介,实现了高速 ...

  7. ElasticSearch 2 (3) - Breaking Changes

    ElasticSearch 2.1.1 (3) - Breaking Changes Search Changes search_type = scan Deprecated GET /my_ind ...

  8. vue 将值存储到vuex 报错问题

    报错 :Vuex - Computed property “name” was assigned to but it has no setter 如何解决: computed: { addModal: ...

  9. ipmitool+python应用处理大量带外地址

    ipmitool 是一种可用在 linux 系统下的命令行方式的 ipmi 平台管理工具,它支持 ipmi 1.5 规范(最新的规范为 ipmi 2.0),通过它可以实现获取传感器的信息.显示系统日志 ...

  10. final 140字评论I

    1.约跑app:优化了最终界面,设备原因画质不是很清晰,如果能加以改进,能有较多的客户群,适合人群不限于青少年和成年人. 2.礼物挑选:虽然界面不是很清晰,但是整体设计看起来还算舒服,最后阶段又新增了 ...