Spring Security是为基于Spring的应用程序提供声明式安全保护的安全性框架。框架下内容比较多,可以做到按照角色权限对请求路径进行限制。今天主要验证自定义登录页,在内存用户存储中进行请求的权限校验。闲话休提,下面直接探讨我的验证过程,如果有比较好的意见,欢迎各位指正。

1、系统使用Maven进行jar包管理,spring用的5.0版本。首先在pom文件中添加Spring Security的jar包依赖

  <dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${security-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${security-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${security-version}</version>
</dependency>

2、添加SecurityConfig 文件,对WebSecurityConfigurerAdapter类进行扩展,重写configure方法。(WebSecurityConfigurerAdapter是对应用中安全框架的个性化定制)

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
/**
* HTTP请求处理
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin().loginPage("/user/login.do")
.defaultSuccessUrl("/free/list.do")//启用FORM登录
.and().authorizeRequests().antMatchers("/user/login.do").permitAll()//登录页允许所有人访问
.and().authorizeRequests().antMatchers("/**/*.do").authenticated()
.and().httpBasic()
.and().csrf().disable(); //暂时禁用CSRF
}
/**
* 授权验证服务
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
/*auth.inMemoryAuthentication()
.withUser("simm").password("{noop}123").roles("USER").and()
.withUser("admin").password("{noop}admin").roles("USER","ADMIN");*/ auth.inMemoryAuthentication().passwordEncoder(NoOpPasswordEncoder.getInstance())
.withUser("simm").password("123").roles("USER").and()
.withUser("admin").password("admin").roles("USER","ADMIN");
}
//.and().requiresChannel().regexMatchers("^((?!/user/login).)*.do").requiresSecure()
//.regexMatchers("^((?!/user/login).)*.do")
}

   备注:新版本的Spring Security要求必须为用户配置提供编码器,否则会报找不到相应的编码器错误。这里有个不是很重要的知识点,假如我们没有调用passwordEncoder方法为用户验证指明编码器,那么有一种替代方案,就是在密码前加"{noop}"等前缀,跟踪源码发现,框架会自动解析{}中的key去匹配相应的编码器。下面提供一个调试的图,可以了解下。

3、添加AppInitializer类,对AbstractSecurityWebApplicationInitializer进行扩展实现,系统启动会自动插入SpringSecurityFilter,完成对请求的安全性拦截。

public class AppInitializer extends AbstractSecurityWebApplicationInitializer   { }

下面截图为AbstractSecurityWebApplicationInitializer中SpringSecurityFilter的启用过程,可做参考。

4、添加登录控制Controller。action接两个可选的请求参数error,logout。当登录失败,框架会重定向到登录页添加一个error参数。当调用/logout 退出系统时,框架在执行完退出事件,清理完身份信息后,重定向回登录页,携带一个logout参数。

@Controller
@RequestMapping("/user")
// 添加session信息的注解,可以实现 session信息与map的映射 赋值
@SessionAttributes("user")
public class UserController {
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(@RequestParam(value = "error", required = false) String error,
@RequestParam(value = "logout", required = false) String logout,Model model) {
if (error != null) {
model.addAttribute("msg", "用户名或密码错误!");
}
if (logout != null) {
model.addAttribute("msg", "成功退出!");
}
return "user/login";
}

5、接下来开始访问系统查看框架应用后的结果

  • 直接访问非登录页,由于没有登录,系统跳转至授权登录页面

  • 验证登录失败,重定向回登录页并自动携带error参数

  • 验证登录成功,重定向到成功页面

  • 点击退出系统

    

安全性框架内容比较广泛,后面将尝试读取数据库中用户,自定义用户校验,https请求,跨站请求伪造等方面的功能。今天的内容比较简单,先作为第一波的敲门砖。

Spring-Security自定义登录页&inMemoryAuthentication验证的更多相关文章

  1. Spring Security 自定义登录认证(二)

    一.前言 本篇文章将讲述Spring Security自定义登录认证校验用户名.密码,自定义密码加密方式,以及在前后端分离的情况下认证失败或成功处理返回json格式数据 温馨小提示:Spring Se ...

  2. (二)spring Security 自定义登录页面与校验用户

    文章目录 配置 security 配置下 MVC 自定义登录页面 自定义一个登陆成功欢迎页面 效果图 小结: 使用 Spring Boot 的快速创建项目功能,勾选上本篇博客需要的功能:web,sec ...

  3. spring boot 下 spring security 自定义登录配置与form-login属性详解

    package zhet.sprintBoot; import org.springframework.beans.factory.annotation.Autowired;import org.sp ...

  4. Spring Boot整合Spring Security自定义登录实战

    本文主要介绍在Spring Boot中整合Spring Security,对于Spring Boot配置及使用不做过多介绍,还不了解的同学可以先学习下Spring Boot. 本demo所用Sprin ...

  5. Spring Security 自定义登录页面

    SpringMVC + Spring Security,自定义登录页面登录验证 学习参考:http://www.mkyong.com/spring-security/spring-security-f ...

  6. spring security采用自定义登录页和退出功能

    更新... 首先采用的是XML配置方式,请先查看  初识Spring security-添加security 在之前的示例中进行代码修改 项目结构如下: 一.修改spring-security.xml ...

  7. Spring Security笔记:自定义登录页

    以下内容参考了 http://www.mkyong.com/spring-security/spring-security-form-login-example/ 接上回,在前面的Hello Worl ...

  8. 【认证与授权】Spring Security自定义页面

    在前面的篇幅中,我们对认证和授权流程大致梳理了一遍.在这个过程中我们一直都是使用系统生成的默认页面,登录成功后也是直接调转到根路径页面.而在实际的开发过程中,我们是需要自定义登录页面的,有时还会添加各 ...

  9. 【JavaEE】SSH+Spring Security自定义Security的部分处理策略

    本文建立在 SSH与Spring Security整合 一文的基础上,从这篇文章的example上做修改,或者从 配置了AOP 的example上做修改皆可.这里主要补充我在实际使用Spring Se ...

随机推荐

  1. Intellij 设置生成serialVersionUID的方法

  2. requests_模拟登录知乎

    如何登录知乎? 首先要分析,进行知乎验证的时候,知乎服务器需要我们提交什么数据,提交的地址.先进行几次登录尝试,通过浏览器中network中查看数据流得知,模拟登录知乎需要提供5个数据,分别是_xsr ...

  3. ORACLE对象大小写问题

    在数据库新建一个测试表(数据库版本为ORACLE 10.2.0.1.0),表名为小写的test. 脚本如下所示: CREATE TABLE test(     id      NUMBER(10),  ...

  4. 带有 thead、tbody 以及 tfoot 元素的 HTML 表格

    设置样式: <head><style type="text/css">thead {color:green}tbody {color:blue;height ...

  5. Halcon一日一练:创建三通道图像

    首先理解一个什么是三通道图像: 三通道图像就是彩色图像,我们之前黑白相机或黑白电视机都是彩用的灰阶图像,即单通道图像,一般是2的8次方个灰阶,即256个灰阶.彩色图像采用RGB,红绿蓝三个通道来合成彩 ...

  6. zabbix监控的基础概念、工作原理及架构

    一.什么是zabbix及优缺点(对比cacti和nagios) Zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系统管理员快速定位/解决存在的各种问题.是一个基于WE ...

  7. 时间转换与星期推算(Matlab版)

    1 概述 最近在学习GPS解算算法时需要在GPS时(GPS周和周内秒)和公历日期之间进行转换,于是就整理了一些时间转换的小程序. 本文介绍了GPS时.公历.儒略日(JD).简化儒略日(MJD)之间的转 ...

  8. 通过重写 class 的 ToString() 来简化获取 enum 的 DescriptionAttribute 值

    通过重写 class 的 ToString() 来简化获取 enum 的 DescriptionAttribute 值 目录 一.常见的 enum 类型 二.演变:class 版本的 enum 类型 ...

  9. 安装supervisor

    机器版本 centos 6.5 python 版本 2.6.6 在终端输入 easy_install supervisor 并回车,linux会自动联网并下载supervisor源码解压并安装 安装成 ...

  10. 20_Python字典总结

    字典: python内置了字典:dic全称dictionary.其他语言中成为map,使用key-value的存储,键-值.具有极快的查找速度类似与我们查字典,我们要查一个字1.从第一页往后翻,知道翻 ...