spring security 安全框架

今天爱分享给大家带来spring security 安全框架,希望能够帮助到大家。

本文以SpringSecurity安全框架为例,为大家分析SpringSecurity安全框架的作用和使用方法,阅读完整文相信大家对SpringSecurity安全框架有了一定的认识。

一、spring security 简介
spring security 的核心功能主要包括:

认证
授权
防护 (防止伪造身份)其核心就是一组过滤器链,项目启动后将会自动配置。最核心的就是 Basic Authentication Filter 用来认证用户的身份,一个在spring security中一种过滤器处理一种认证方式。

比如,对于username password认证过滤器来说, 会检查是否是一个登录请求;是否包含username 和 password (也就是该过滤器需要的一些认证信息) ;如果不满足则放行给下一个。

下一个按照自身职责判定是否是自身需要的信息,basic的特征就是在请求头中有 Authorization:Basic eHh5Onh5 的信息。中间可能还有更多的认证过滤器。最后一环是 FilterSecurityInterceptor,这里会判定该请求是否能进行访问rest服务,判断的依据是 BrowserSecurityConfig中的配置,如果被拒绝了就会抛出不同的异常(根据具体的原因)。Exception Translation Filter 会捕获抛出的错误,然后根据不同的认证方式进行信息的返回提示。

注意:绿色的过滤器可以配置是否生效,其他的都不能控制。

二、SpringSecurity入门实战
创建一个普通的maven工程,打包方式为war
在 src/main/resources中加入如下文件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: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://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

    <!-- 设置页面不登录也可以访问 -->
    <http pattern="/login.html" security="none"></http>
    <http pattern="/login_error.html" security="none"></http>

    <!-- 配置页面的拦截规则  use-expressions="false"是否启用SPEL表达式-->               
    <http use-expressions="false">
    <!-- 当前应乎必须属于ROLE_USER这个角色,才可以访问根目录以及所属子目录的资源 -->
        <intercept-url pattern="/**" access="ROLE_USER" />
        <!-- 开启表单登录的功能 -->
        <form-login login-page="/login.html" default-target-url="/index.html" authentication-failure-url="/login_error.html"/>
        <csrf disabled="true"/>
    </http>

    <!-- 认证管理器 -->
    <authentication-manager>
        <authentication-provider>
            <user-service>
               <!--表示配置用户属于ROLE_USER并且配置用户登陆的密码和账号-->
                <user name="admin" password="123456" authorities="ROLE_USER"/>
            </user-service>
        </authentication-provider>
    </authentication-manager>

这里的很多东西都有注解了,说一下没有注解的标签,form-login标签表示配置一个登陆的功能,login-page=”/login.html”表示配置我们自己登陆的页面路径,若是没有配置form-login这个标签,SpringSecurity会自动帮我们生成一个登陆的页面,但是都不会用SpringSecurity给我们生成的登录页面,authentication-failure-url标示密码或者账号错误跳转的页面,default-target-url表示密码或者账号正确登陆后跳转的页面,表示关闭csrf

3. 配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-security.xml</param-value>
 </context-param>
 <listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
 </listener>

 <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的配置文件而已,相信很多人都能看懂
4. 创建html页面

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h2>欢迎进入进入神奇的Spring-Security世界</h2>
</body>
</html>

login_error.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>用户名密码错误</h2>
</body>
</html>

login.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>

--欢迎登录我的系统--
<form action="/login" method="post">
用户名:<input name="username"><br>
密码:<input name="password"><br>
<button>登录</button>
</form>

</body>
</html>

人已赞赏
Java

编程是一门艺术吗?[完美解释]

2020-9-27 17:39:06

JavaLinux

linux 安装Java JDK 步骤详解【图文并茂】

2020-10-15 17:13:30

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
'); })();