springSecurity入门小demo

1.添加依赖

 <!--添加springSecurity的依赖-->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>4.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>4.1.0.RELEASE</version>
        </dependency>
<!--springSecurity的依赖-->

2.配置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 security="none" pattern="/login.html"></http>
	<http security="none" pattern="/authenticationError.html"></http>
	<!--配置拦截规则-->
	<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="/authenticationError.html"/>
		<!--关闭csrf-->
		<csrf disabled="true"></csrf>
	</http>
	<!--配置认证管理器-->
	<authentication-manager>
		<authentication-provider>
			<user-service >
				<user name="admin" password="12345" authorities="ROLE_USER"></user>
			</user-service>
		</authentication-provider>
	</authentication-manager>
</beans:beans>

3.在web.xml文件中配置spring-security的过滤器

<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>

4.创建配置文件中的相关页面

猜你喜欢

转载自blog.csdn.net/ZQQ8015/article/details/87538181