12-17 程宗武
Spring Security
Spring Security 的前身是 Acegi Security ,是 Spring 项目组中用来提供安全认证服务的框架。
安全主要包含两个操作:
“认证”,是为用户建立一个他所声明的主体。主题一般式指用户,设备或可以在你系统中执行动作的其他系统。
“授权”指的是一个用户能否在你的应用中执行某个操作,在到达授权判断之前,身份的主题已经由身份验证过程建立了。
1 Spring Security 入门
1.1 添加Maven依赖
<!-- spring-security -->
<!-- 包含过滤器和相关的Web安全基础结构代码,任何与servlet API依赖的东西 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 包含安全名称空间解析代码和Java配置代码 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.version}</version>
</dependency>
1.2 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">
<!--auto-config设置为true如果我们没有指定登录页面,会为我们自动创建一个登录页面-->
<security:http auto-config="true" use-expressions="false">
<!--定义访问页面所需要的角色信息-->
<security:intercept-url pattern="/**" access="ROLE_USER,ROLE_ADMIN"/>
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<!--配置角色信息-->
<security:user name="creep" authorities="ROLE_USER" password="{noop}1234"/>
<security:user name="admin" authorities="ROLE_ADMIN"
password="{bcrypt}$2a$10$pwbWHHvmFFQkaCJLoCtFBOgu9Y27szSgoASfvhTh.7BV0QvayP5ya"/>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans>
1.3 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--过滤器,拦截请求,跳转到登录页面-->
<filter>
<!--name是固定的,因为会根据这个name找到对应的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>
<!--加载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>
<!--配置DispatcherServlet-->
<servlet>
<servlet-name>disPatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置Tomcat启动时就加载spring配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
<!--配置启动顺序-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--所有请求都会先经过DispatcherServlet进行处理-->
<servlet-mapping>
<servlet-name>disPatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2.自定义登录页面
<!-- 配置不过滤的资源(静态资源及登录相关) -->
<security:http security="none" pattern="/login.jsp" />
<security:http security="none" pattern="/failer.jsp" />
<security:form-login login-page="/login.jsp"
login-processing-url="/login" usernameparameter="username"
password-parameter="password" authenticationfailure-url="/failer.jsp"
default-target-url="/success.jsp"/>
<!--退出登录invalidate-session="true"会删除session中保存的用户信息 -->
<security:logout invalidate-session="true" logout-url="/logout"
logout-success-url="/login.jsp" />
<!-- 关闭CSRF, 用于指定禁用Spring Security的CSRF保护。默认值为false(启用CSRF保护)-->
<security:csrf disabled="true" />
3.Spring Security使用数据库认证
点赞
评论