Spring Security 简单入门|学习笔记

简介: 快速学习 Spring Security 简单入门

开发者学堂课程【Spring Security知识精讲与实战演示(一)Spring Security 简单入门】学习笔记与课程紧密联系,让用户快速学习知识

课程地址https://developer.aliyun.com/learning/course/730/detail/13030


Spring Security 简单入门

 

内容介绍:

一、配置 web.xml

二、配置 spring-security.xml

三、将spring-security.xml 配置文件引入到applicationContext.xml 中

四、运行结果

 

一、配置web.xml

编写 spring-security 的核心过滤器链代码。

<web-app xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://lww.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

Version = "3.0" >

<display-name>Archetype Created WebApplication</display-name>

<! - Spring Security 过滤器链,注意过滤器名称必须叫 springSecurityFilterChain-->

<!--SpringSecurity 核心过滤器链-->

<!--SpringSecurityFilterChina 名称不能修改-->

<filter >

<filter-name>springSecurityFilterChain</filter-name>

<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>

</filter>

<filter-mapping>

<filter-name>springSecurityFilterChain</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

</web-app>

 

二、配置 spring-security.xml

<? xml version ="1.e” encoding =" UTF -8"?>

< beans xmlns =“ http :/// wMW . springframework . org / schema / beans "

xmlns : xsi ="http://wow.w3.org/2001/XMLSchema- instance "

xm1ns: context =“http://www. springframework . org / schema / context ”

xmlns : aop =“http://wwW. springframework . org / schema / aop "

xmlns : tx =“http://www. springframework . org / schema / tx ”

xmlns : security =“ http :/// wm . springframework . org / schema / security ”

xsi : schemaLocation =“ http :/// www . springframework . org / schema / beans

http://Mow. springframewok . org / schema / beans / spring - beans . xsd

http://www. springframework . org / schema / context

http://www. springframework . org / schema / context / spring - context . xsd

http://ww. springframework . org / schema / aop

http://wwW. springframework . org / schema / aop / spring - aopxsd

http://wwW. springframework . org / schema / tx

http://www. springframework , org / schema / tx / spring - tx . xsd

http :/// wwW . springframework . org / schema / security

http://www. springframework . org / schema / security / spring - security - xsd ">

以下配置非常重要,若没有这些配置讲运行不了程序。

<!--配置 springSecurity-->

<!--

auto-confing=“true”表示自动加载 springsecurity 的配置文件>

use-expressions=”true”表示使用 springsecurity 的 el 表达式来配置springseccyrity

-->

< security : http auto - config -“ true ” use - expressions =" true ">

<!--拦截资源-->

<!-﹣使用 spring 的 el 表达式来指定项目所有资源访问都必须有 ROLE USER 或 ROLE _ ADMIN 角色﹣->

<!--

<!-- pattern ="/**”表示拦截所有资源

access =“ hasAnyRole (' ROLE _ USER ')表示只有ROLE-USER角色才能访问资源

-->

< security : intercept - url pattern ="/**” access =“ hasAnyRole (' ROLE _ USER ',' ROLE _ ADMIN ‘)

</ security : http >

HasRole 只能写一个决策,HasRole 可以写多个决策,这里选择HasRole;为了测试方便,选择跟数据库保持一致选择 ROLE USER;pattern ="/**” 中的第一个星表示路径,第二个表示子目录以及其后面所带的参数;

现在 ROLE_USER 的这样角色还能访问所有资源?怎样系统才能拿到角色?需要要认证,那么要认证就必须有用户名和密码。需要先来一个非常简单的入门,暂时不连数据库。在内存中模拟两个临时用户。点查模拟临时用户的这端配置直接贴过来。后面对它进行优化。

<!--设置 Spring Security 认证用户信息的来源﹣->

<!--

Springsecyrity默认的认证必须是加密的,加上{noop}表示不加密认证.

-->

< security : authentication - manager >

< security : authentication - provider >

< security : user - service >

< security : user name -" user ” password -"[ noop } user ”authorities =“ ROLE _ USER ”/>

< security : user name =" admin " password ="[ noop } admin "authorities =" ROLE _ ADMIN ”/>

</ security : user - service >

</ security : authentication - provider ></ security : authentication - manager >

</ beans >

一共模拟了两个用户,一个是 user,一个是 admin,user 的密码是 user,admin的密码是admin,user的角色是 ROLE-USER,admin 的角色是 ROLE-ADMIN,。 no o p 是什么意思?要注意是 Spring Security 的认证必须是加密的。启动外部工程,是不是只会读取外部的所有信息?而外部 ROLE 会自动加载 applicationContext?很明显是不会;将 spring-security.xml 配置文件引入到 applicationContext.xml 中。

 

三、将spring-security.xml配置文件引入到applicationContext.xml中

<!-﹣引入 SpringSecurity 主配置文件﹣->

< import resource =“ classpath : spring - security . xml “/>

外部工程有=两个容器,一个是复容器一个是子容器;子容器可以访问复容器的内容,但复容器不可以访问子容器的内容,但是对于外界,它只能访问子容器。比如:复容器就相当于整个中国,而子容器就相当于中国开放的一个关口,如果国外的人想访问中国的资源,就必须通过这个关口来访问作呀,否则将不能访问;如果国外的人想访这些资源,它可以通过子容器去取调用。随着 applicationContext 一起交代,将 springsecuruty 配置文件引入到 spring 文件中。

 

四、运行结果

以下页面是错误,springsecurity 提供,没有提供认证所致;

image.png

当启动项目时,按理用启动index.jsp,点开配置文件,配置了/**它包含了index页面,所以要访问index必要要访问ROLE-USER的角色,可是现在没有这个角色,要认证通过才有,所以继续用user认证,所以没有这个角色,spring会自动跳转到它自己提供的页面。

F12如下图,是个简单的报表单,可以看到用户名和密码,注意隐藏项“_csrf”跨域所致登录页面误差;

 image.png

看控制台异常信息,因权限不足,内部定义;

image.png

加载信息,有一大堆的过滤器链。

image.png

相关文章
|
13天前
|
安全 Java 数据安全/隐私保护
|
11天前
|
Java 数据安全/隐私保护 Sentinel
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
|
12天前
|
Java Nacos 开发者
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
|
12天前
|
Dubbo Java 应用服务中间件
Java从入门到精通:3.2.2分布式与并发编程——了解分布式系统的基本概念,学习使用Dubbo、Spring Cloud等分布式框架
Java从入门到精通:3.2.2分布式与并发编程——了解分布式系统的基本概念,学习使用Dubbo、Spring Cloud等分布式框架
|
17天前
|
存储 安全 Java
第10章 Spring Security 的未来趋势与高级话题(2024 最新版)(下)
第10章 Spring Security 的未来趋势与高级话题(2024 最新版)
21 2
|
17天前
|
安全 Cloud Native Java
第10章 Spring Security 的未来趋势与高级话题(2024 最新版)(上)
第10章 Spring Security 的未来趋势与高级话题(2024 最新版)
26 2
|
17天前
|
存储 安全 Java
第9章 Spring Security 的测试与维护 (2024 最新版)(下)
第9章 Spring Security 的测试与维护 (2024 最新版)
20 1
|
17天前
|
安全 Java 测试技术
第9章 Spring Security 的测试与维护 (2024 最新版)(上)
第9章 Spring Security 的测试与维护 (2024 最新版)
21 0
|
17天前
|
缓存 Java 数据库
第8章 Spring Security 的常见问题与解决方案(2024 最新版)(下)
第8章 Spring Security 的常见问题与解决方案(2024 最新版)
25 0
|
17天前
|
安全 Java 数据安全/隐私保护
第8章 Spring Security 的常见问题与解决方案(2024 最新版)(上)
第8章 Spring Security 的常见问题与解决方案(2024 最新版)
33 0