1 功能需求

  springSecuriy默认的登录窗口是一个弹出窗口,而且会默认对所有的请求都进行拦截;要求更改登录页面(使用表单登录)、排除掉一些请求的拦截

2 编写一个springSecurity配置类

  继承 WebSecurityConfigurerAdapter 类,并重写两个configure方法   

package cn.xiangxu.spring_security_system;

//import cn.xiangxu.spring_security_system.service.MyUserService;
import cn.xiangxu.spring_security_system.utils.MyPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; /**
* 权限服务类
*/
@Configuration // 等同于XML中的beans
@EnableWebSecurity // 开启webSecurity功能
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { // @Autowired
// private MyUserService myUserService; // @Override
// protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//
// // 直接将用户信息和权限写死
//// auth.inMemoryAuthentication()
//// .withUser("admin")
//// .password("123456")
//// .roles("ADMIN");
//// auth.inMemoryAuthentication()
//// .withUser("wys")
//// .password("123456")
//// .roles("ADMIN");
//// auth.inMemoryAuthentication()
//// .withUser("demo")
//// .password("123456")
//// .roles("USER");
//
// auth.userDetailsService(myUserService).passwordEncoder(new MyPasswordEncoder()); // 利用自定义的UserService进行管理
// } @Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").permitAll() // 主页面请求拦截排除
.anyRequest().authenticated() // 除主页面外的所有请求都会被拦截
.and()
.logout().permitAll() // 注销请求拦截排除
.and()
.formLogin(); // 设置使用表单登录的方式
http.csrf().disable(); // 关闭csrf验证
} @Override
public void configure(WebSecurity web) throws Exception {
// 拦截排除设置
web.ignoring().antMatchers("/js/**", "/css/**", "/images/**");
} }

权限配置类

  技巧01:@Configuration 就相当于xml配置文件中的beans,@Bean就相当于XML配置文件中的bean

  

3 编写一些接口用于测试

  注意:为了简便,我直接将测试接口写在了启动类中

package cn.xiangxu.spring_security_system;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @SpringBootApplication
@RestController
//@EnableGlobalMethodSecurity(prePostEnabled = true) // 开启@PreAuthorize注解
public class SpringSecuritySystemApplication { public static void main(String[] args) {
SpringApplication.run(SpringSecuritySystemApplication.class, args);
} @GetMapping(value = "/")
public String home() {
return "Welcome to study springSecurity.";
} @GetMapping(value = "/hello")
public String hello() {
return "hello boy";
} // @PreAuthorize("hasRole('ROLE_ADMIN')") // 设定权限校验:只用ADMIN角色才能调用该接口
@GetMapping("/roleAuth")
public String role() {
return "admin role";
}
}

  3.1 访问主页面 http://127.0.0.1:8080/ 时跳过了登录验证

    原因:我们在springSecurity配置类中排除了 http://127.0.0.1:8080/ 请求(即:http://127.0.0.1:8080/ 会默认不进行登录验证)

    

  3.2 访问hello页面 http://127.0.0.1:8080/hello 时自动跳转到了登录页面

    技巧01:登录名默认是user,登录密码在控制台打印出来了

    

SpringSecurity02 表单登录、SpringSecurity配置类的更多相关文章

  1. SpringSecurity 自定义表单登录

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

  2. SpringSecurity实战记录(一)开胃菜:基于内存的表单登录小Demo搭建

    Ps:本次搭建基于Maven管理工具的版本,Gradle版本可以通过gradle init --type pom命令在pom.xml路径下转化为Gradle版本(如下图) (1)构建工具IDEA In ...

  3. SpringSecurity 默认表单登录页展示流程源码

    SpringSecurity 默认表单登录页展示流程源码 本篇主要讲解 SpringSecurity提供的默认表单登录页 它是如何展示的的流程, 涉及 1.FilterSecurityIntercep ...

  4. SpringBoot集成Spring Security(4)——自定义表单登录

    通过前面三篇文章,你应该大致了解了 Spring Security 的流程.你应该发现了,真正的 login 请求是由 Spring Security 帮我们处理的,那么我们如何实现自定义表单登录呢, ...

  5. Spring Security 表单登录

    1. 简介 本文将重点介绍使用Spring Security登录. 本文将构建在之前简单的Spring MVC示例之上,因为这是设置Web应用程序和登录机制的必不可少的. 2. Maven 依赖 要将 ...

  6. spring security 之自定义表单登录源码跟踪

    ​ 上一节我们跟踪了security的默认登录页的源码,可以参考这里:https://www.cnblogs.com/process-h/p/15522267.html 这节我们来看看如何自定义单表认 ...

  7. VC POST表单——登录验证新浪邮箱

    1.本机环境: Windows XP SP3.ADSL 2.开发工具: WildPackets OmniPeek V5.1.4 Visual C++ 6.0 IE6.0 FlexEdit V2.3.1 ...

  8. Android逆向破解表单登录程序

    Android逆向破解表单登录程序 Android开发 ADT: android studio(as) 程序界面如下,登录成功时弹出通知登录成功,登录失败时弹出通知登录失败. 布局代码 <?xm ...

  9. Confluence 6 管理员联系表单的后台配置界面

    管理员联系表单的后台配置界面截图和配置. 对输入的数据进行编辑和选择是否启用发送电子邮件给管理员 https://www.cwiki.us/display/CONFLUENCEWIKI/Configu ...

随机推荐

  1. Hadoop如何修改HDFS文件存储块大小

    一. 临时修改可以在执行上传文件命令时,显示地指定存储的块大小.1. 查看当前 HDFS文件块大小我这里查看HDFS上的TEST目录下的jdk-7u25-linux-x64.gz  文件存储块大小.1 ...

  2. 第三章 python中的字符串

    一.字符串的基本操作 所有标准的序列操作对字符串同样适用,如索引.分片.乘法.判断成员是否存在.求长度.最大值和最小值等.记住一点,字符串是不可变的. 二.字符串中重要的方法 1.find(subst ...

  3. dedecms常用标签

    下面总结了58种常见的标签调用,包括关键描述调用.指定调用栏目.列表文章调用.频道栏目调用.当前栏目名称.栏目导航调用.模板路径调用.网站标题调用.友情链接调用.网站版权调用.网站备案调用.当前位置调 ...

  4. Python 案例一(计算人体体脂率)

    #计算人体体脂率 #输入部分 #身高 personHeight = input("请输入你的身高(m):") personHeight = float(personHeight) ...

  5. artdialog插件--iframe穿透特性

    使用artdialog可以实现嵌套页面间的通信. 一.引入插件 //artdialog是建立在jquery上面的所以要首先引入jquery <script src="__CLASSTP ...

  6. 分享知识-快乐自己:Liunx 搭建 Dubbo

    1.首先配置JDK  操作步骤 2.部署 Tomcat ① 上传 Tomcat 7  解压jdk文件:tar -zxvf jdk文件名称 ② tomcat目录下的bin/启动tomcat ③ tail ...

  7. 分享知识-快乐自己:MyBtis内置缓存机制

    在实际的项目开发中,通常对数据库的查询性能要求很高,而mybatis提供了查询缓存来缓存数据,从而达到提高查询性能的要求. mybatis的查询缓存分为一级缓存和二级缓存,一级缓存是SqlSessio ...

  8. node.js+express+jade系列七:富文本编辑框的使用

    下载nicEdit富文本编辑框, 把nicEdit.js文件放到public/javascripts/下 新建jade文件:代码如下 doctype htmlhtml    head        t ...

  9. 关于COM组件调用

    转载自:http://www.cppblog.com/ice197983/articles/4178.html 一.调用步骤: 使用ATL编写的COM组件调用方法有两种:1.导入myCom.dll文件 ...

  10. Java读取文件的时候,如何让指针重新回到文件的开头

    今天在测试IO流的使用的时候发现在reader读取文件之后,再向文件添加内容,再继续读文件,打印出的结果只能读取追加的文件. 如何才能重新读取呢?试了mark和reset,似乎会报异常.记在这以后看是 ...