在一个系统中认证和授权是常有的事情,现在比较流行的框架有spring security、shiro等等。他们都能很好的帮助我们完成认证和授权的功能。那么假如说让我们自己完成一个登录那么应该大致的流程是怎么样的呢?

、我们肯定有一个处理页面上表单提交的url处理器,此处我们放置在Filter中进行处理,假如就叫做UsernamePasswordAuthenticationFilter  ,在这个Filter里面我们需要将用户提交上来的用户名和密码封装成一个对象,假如叫做UsernamePasswordAuthenticationToken。

、有了上一步的 UsernamePasswordAuthenticationToken 之后,我们肯定要对这个token进行一个认证,那UsernamePasswordAuthenticationFilter中就必须要有一个认证管理器(AuthenticationManager),来处理我们这个token的认证。

、有了认证管理器后,那这个认证管理器可能可以处理多种方式的登录,比如基于ip地址的登录、基于用户名和密码的登录、基于remember me(记住我)的登录等等。那么在我们的认证管理器的实现中(ProviderManager)就应该存在一组AuthenticationProvider,每个AuthenticationProvider处理不同的Token认证。

比如:DaoAuthenticationProvider用于处理UsernamePasswordAuthenticationToken

RememberMeAuthenticationProvider用于处理RememberMeAuthenticationToken

 四、当我们是基于用户名和密码登录认证时,那么对应的AuthenticationProvider就是DaoAuthenticationProvider,   那么我们肯定有一个接口根据用户输入的用户名返回一个用户对象即 UserDetailsService#loadUserByUsername(String) 接口,返回一个UserDetails 对象,有了用户对象后,我们系统中密码肯定是加密过的,那么就必须还要一个密码加密器PasswordEncoder,用于进行密码的校验。

、当完成上面的认证后

成功认证:那么肯定有一个成功后的回调,加入是 AuthenticationFailureHandler , 在这个里面我们可以完成成功后页面的跳转

认证失败:也有一个失败的回调,假如是 AuthenticationFailureHandler,可以记录失败的日志,完成失败后的跳转

有了上面的初步认识后,我们来看一下在spring security中是如何做的。

一、引入spring security的pom配置文件

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Brussels-SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

二、进行spring security的配置

/**
* spring security 的配置
*
* @描述
* @作者 huan
* @时间 2017年11月1日 - 下午9:33:59
*/
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(createUserDetailService())//
.passwordEncoder(passwordEncoder())//
.and()//
.inMemoryAuthentication()//
.withUser("admin")
.password("admin")
.roles("ADMIN");// 配置一个拥有用户名和密码的用户
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin() // 表单登录
.loginProcessingUrl("/login")// 处理登录的请求
.loginPage("/login.html") // 自定义登录界面
.usernameParameter("authUsername")//登录表单的用户名的name的值
.passwordParameter("authPassword")//登录表单密码的name的值
.successHandler(securityAuthenticationSuccessHandler())// 认证成功后的处理
.failureHandler(securityAuthenticationFailureHandler()) // 认证失败后的处理
.and()//
.userDetailsService(createUserDetailService()) // 用户服务,根据用户名加载用户信息
.logout() // 登出
.logoutUrl("/logout") // 登出的url路径
.addLogoutHandler(securityLogoutHandler())//登出时的处理操作
.clearAuthentication(true)// 清除认证
.invalidateHttpSession(true)// session失效
.and()//
.csrf() // csrf
.disable()//
.authorizeRequests()//
.antMatchers("/login.html","/login").permitAll() // 可匿名访问
.anyRequest().authenticated() // 除了上方的请求,其余的请求需要认证才可以访问
.and()//
.exceptionHandling()//
.accessDeniedPage("/403.html"); // 没有权限访问的页面
} @Bean
public AuthenticationFailureHandler securityAuthenticationFailureHandler() {
SecurityAuthenticationFailureHandler authenticationFailureHandler = new SecurityAuthenticationFailureHandler();
authenticationFailureHandler.setDefaultFailureUrl("/login.html?error");
return authenticationFailureHandler;
} @Bean
public AuthenticationSuccessHandler securityAuthenticationSuccessHandler() {
SecurityAuthenticationSuccessHandler authenticationSuccessHandler = new SecurityAuthenticationSuccessHandler();
authenticationSuccessHandler.setAlwaysUseDefaultTargetUrl(true);
authenticationSuccessHandler.setDefaultTargetUrl("/index.html");
return authenticationSuccessHandler;
} @Bean
public LogoutHandler securityLogoutHandler() {
return new SecurityLogoutHandler();
} @Bean
public UserDetailsService createUserDetailService() {
return new SecurityUserDetailServiceImpl(passwordEncoder());
} /** 密码加密器 */
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}

三、各个实体类

 四、登录页

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<body>
<form action="/login" method="post">
用户名:<input type="text" name="authUsername" /><br />
密码:<input type="password" name="authPassword" /><br/>
<input type="submit" value="登录">
</form>
</body>
</html>

五、首页

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>
<link rel="stylesheet" type="text/css" href="/static/css/index.css">
</head>
<body>
<h1>
登录成功。<a href="/logout">退出</a>
</h1>
</body>
</html>

六、界面效果

登录页

登录成功后的首页

认识spring security的更多相关文章

  1. Spring Security OAuth2 开发指南

    官方原文:http://projects.spring.io/spring-security-oauth/docs/oauth2.html 翻译及修改补充:Alex Liao. 转载请注明来源:htt ...

  2. spring mvc 和spring security配置 web.xml设置

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmln ...

  3. SPRING SECURITY JAVA配置:Web Security

    在前一篇,我已经介绍了Spring Security Java配置,也概括的介绍了一下这个项目方方面面.在这篇文章中,我们来看一看一个简单的基于web security配置的例子.之后我们再来作更多的 ...

  4. 【OAuth2.0】Spring Security OAuth2.0篇之初识

    不吐不快 因为项目需求开始接触OAuth2.0授权协议.断断续续接触了有两周左右的时间.不得不吐槽的,依然是自己的学习习惯问题,总是着急想了解一切,习惯性地钻牛角尖去理解小的细节,而不是从宏观上去掌握 ...

  5. spring security oauth2.0 实现

    oauth应该属于security的一部分.关于oauth的的相关知识可以查看阮一峰的文章:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html ...

  6. Spring Security(08)——intercept-url配置

    http://elim.iteye.com/blog/2161056 Spring Security(08)--intercept-url配置 博客分类: spring Security Spring ...

  7. Spring Security控制权限

    Spring Security控制权限 1,配置过滤器 为了在项目中使用Spring Security控制权限,首先要在web.xml中配置过滤器,这样我们就可以控制对这个项目的每个请求了. < ...

  8. Spring Security笔记:Hello World

    本文演示了Spring Security的最最基本用法,二个页面(或理解成二个url),一个需要登录认证后才能访问(比如:../admin/),一个可匿名访问(比如:../welcome) 注:以下内 ...

  9. Spring Security笔记:自定义Login/Logout Filter、AuthenticationProvider、AuthenticationToken

    在前面的学习中,配置文件中的<http>...</http>都是采用的auto-config="true"这种自动配置模式,根据Spring Securit ...

  10. spring session 和 spring security整合

    背景: 我要做的系统前面放置zuul. 使用自己公司提供的单点登录服务.后面的业务应用也是spring boot支撑的rest服务. 目标: 使用spring security管理权限包括权限.用户请 ...

随机推荐

  1. kernel_thread()和kthread_run()/kthread_create()的根本区别

    0 本质区别 kthread_run()调用kthread_create(), kthread_create()加入链表后,有kthreadd()线程读取链表然后再调用kernel_thread()创 ...

  2. finally方法体

    1.资源释放 java7可以在try(创建资源对象,方法体结束之后自动释放) 2.finally中有返回

  3. shell中的$0 $n $# $* $@ $? $$

    $0当前脚本的文件名 $n传递给脚本或函数的参数.n 是一个数字,表示第几个参数.例如,第一个参数是$1,第二个参数是$2. $#传递给脚本或函数的参数个数. $*传递给脚本或函数的所有参数. $@传 ...

  4. Java跨平台原理(字节码文件,虚拟机)

    介绍 C/C++语言都直接编译成针对特定平台机器码.如果要跨平台,需要使用相应的编译器重新编译. Java源程序(.java)要先编译成与平台无关的字节码文件(.class),然后字节码文件再解释成机 ...

  5. php获取纳秒方法

    PHP不提供精度高于微秒的函数. 可以使用system功能,直接从机器中获得的价值,如果你运行的是Linux: $nanotime = system('date +%s%N');

  6. Java基础系列(17)- 顺序结构

    顺序结构 JAVA的基本结构就是顺序结构,除非特别说明,否则就按照顺序一句一句执行 顺序结构是最简单的算法结构 语句与语句之间,框与框之间是按从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的 ...

  7. Linux系列(14) - grep

    简述 grep是在文件当中匹配符合条件的字符串,作用是查找文件内容 格式 grep [选项] 字符串 文件名 选项 -i:忽略大小写 -v:排除指定字符串 -n:显示行号 例子 grep " ...

  8. python刷题第二周

    1: 第3章-5 字符转换 (15 分) 本题要求提取一个字符串中的所有数字字符('0'--'9'),将其转换为一个整数输出. 输入格式: 输入在一行中给出一个不超过80个字符且以回车结束的字符串. ...

  9. php nginx 路径批量配置

    * 假设 E:\upload 作为图片上传的位置 nginx 做web服务 * 创建文件conf.php 放到这个目录下 <?php function handleDir($it, &$ ...

  10. 字体图标Icon Font

    字体图标Icon Font 前段时间研究怎样做字体图标,在网上查找诸多资料,诸多尝试,找到一套可以自己制作自己独立控制的制作流程,公司按照这套流程形成一套自己公司图标,本人目前所在公司已经在使用没有发 ...