SpringSecurity no1.

SpringSecurity 安全框架

前言:类似的安全框架有shrio等

做权限效验的框架

数据库设计:

一般来说都是主要的三张表

一个是:用户表
二个是:角色表
三个是:资源权限表

表设计如下
三张表 多对多 产生的关联表
在这里插入图片描述

开发入门:

导入依赖

<properties>
		<spring.version>5.0.2.RELEASE</spring.version>
		<spring.security.version>5.0.1.RELEASE</spring.security.version>
</properties>
<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-web</artifactId>
			<version>${spring.security.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-config</artifactId>
			<version>${spring.security.version}</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
			<scope>provided</scope>
		</dependency>

配置文件:spring-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:security="http://www.springframework.org/schema/security"
	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">        
          
	
	<!-- 配置不过滤的资源(静态资源及登录相关) -->
	<security:http security="none" pattern="/login.html" />
	<security:http security="none" pattern="/failer.html" />
	<security:http auto-config="true" use-expressions="false" >
		<!-- 配置资料连接,表示任意路径都需要ROLE_USER权限 -->
		<security:intercept-url pattern="/**" access="ROLE_USER" />
		<!-- 自定义登陆页面,login-page 自定义登陆页面 authentication-failure-url 用户权限校验失败之后才会跳转到这个页面,如果数据库中没有这个用户则不会跳转到这个页面。 
			default-target-url 登陆成功后跳转的页面。 注:登陆页面用户名固定 username,密码 password,action:login -->
		<security:form-login login-page="/login.html"
			login-processing-url="/login" username-parameter="username"
			password-parameter="password" authentication-failure-url="/failer.html"
			default-target-url="/success.html" 
			/>
		<!-- 登出, invalidate-session 是否删除session logout-url:登出处理链接 logout-success-url:登出成功页面 
			注:登出操作 只需要链接到 logout即可登出当前用户 -->
		<security:logout invalidate-session="true" logout-url="/logout"
			logout-success-url="/login.jsp" />
		<!-- 关闭CSRF,默认是开启的 -->
		<security:csrf disabled="true" />
	</security:http>
	<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>

配置拦截器: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">
	<display-name>SpringSecurity314</display-name>

	<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>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

SpringSecurity 使用数据库认证

SpringSecurity 其实就是接管了controller.

发布了76 篇原创文章 · 获赞 9 · 访问量 6765

猜你喜欢

转载自blog.csdn.net/qq_37870369/article/details/99842500