权限操作-springSecurity快速入门
生活随笔
收集整理的這篇文章主要介紹了
权限操作-springSecurity快速入门
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Spring Security快速入門
pom.xml
<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>web.xml
<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>spring security配置
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/securityhttp://www.springframework.org/schema/security/spring-security.xsd"><security:http auto-config="true" use-expressions="false"><!-- intercept-url定義一個過濾規(guī)則 pattern表示對哪些url進(jìn)行權(quán)限控制,ccess屬性表示在請求對應(yīng)的URL時需要什么權(quán)限,默認(rèn)配置時它應(yīng)該是一個以逗號分隔的角色列表,請求的用戶只需擁有其中的一個角色就能成功訪問對應(yīng)的URL --><security:intercept-url pattern="/**" access="ROLE_USER" /><!-- auto-config配置后,不需要在配置下面信息 <security:form-login /> 定義登錄表單信息<security:http-basic/> <security:logout /> --></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>測試
我們在webapp下創(chuàng)建一個index.html頁面,在頁面中任意寫些內(nèi)容。
當(dāng)我們訪問index.html頁面時發(fā)現(xiàn)會彈出登錄窗口,可能你會奇怪,我們沒有建立下面的登錄頁面,為什么SpringSecurity會跳到上面的登錄頁面呢?這是我們設(shè)置http的auto-config=”true”時Spring Security自動為我們生成的。
總結(jié)
以上是生活随笔為你收集整理的权限操作-springSecurity快速入门的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 权限操作-springSecurity概
- 下一篇: 权限操作-springSecurity快