一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 詳解Spring Security 簡單配置

詳解Spring Security 簡單配置

2020-10-29 14:59憨厚的老菜鳥 Java教程

本篇文章主要介紹了詳解Spring Security 簡單配置,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

開發(fā)環(huán)境

  1. maven
  2. idea
  3. jdk 1.8
  4. tomcat 8

配置spring mvc + spring security

pom.xml

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<properties>
 <spring.version>4.3.8.RELEASE</spring.version>
 <spring-sercurity.version>4.2.2.RELEASE</spring-sercurity.version>
</properties>
 
 
<dependencies>
 <!-- Spring 4 dependencies -->
 
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>${spring.version}</version>
 
 </dependency>
 
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>${spring.version}</version>
 </dependency>
 
 <!-- Spring Security -->
 <dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-core</artifactId>
  <version>${spring-sercurity.version}</version>
 </dependency>
 
 <dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-web</artifactId>
  <version>${spring-sercurity.version}</version>
 </dependency>
 
 <dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-config</artifactId>
  <version>${spring-sercurity.version}</version>
 </dependency>
</dependencies>

spring mvc 使用的是4.3.8版本,spring security 使用的是4.2.2版本。

spring-mvc-servlet.xml

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  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-4.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 
 <context:component-scan base-package="com.controller" />
 
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix">
   <value>/WEB-INF/pages/</value>
  </property>
  <property name="suffix">
   <value>.jsp</value>
  </property>
 </bean>
</beans>

spring-security.xml

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?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:p="http://www.springframework.org/schema/p"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security.xsd">
 
 <http auto-config="true" >
  <intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>
 </http>
 
 <authentication-manager>
  <authentication-provider>
   <user-service>
    <user name="admin" password="123456" authorities="ROLE_USER"/>
   </user-service>
  </authentication-provider>
 </authentication-manager>
</beans:beans>

web.xml

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?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_3_0.xsd"
   id="WebApp_ID" version="3.0">
 <display-name>Archetype Created Web Application</display-name>
 
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
   classpath:spring-mvc-servlet.xml,
   classpath:spring-security.xml
  </param-value>
 </context-param>
 
 <!-- spring mvc -->
 <servlet>
  <servlet-name>spring-mvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath*:spring-mvc-servlet.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>spring-mvc</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
 
 <!-- spring security -->
 <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.jsp</welcome-file>
 </welcome-file-list>
</web-app>

測試

spring會攔截所有請求,如果沒有登錄,則系統(tǒng)會跳轉(zhuǎn)到spring security默認(rèn)的登錄頁面。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.jianshu.com/p/faf877265964#

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲天堂网站在线 | 精品久久国产 | 男人天堂网页 | 色中文字幕 | 奶大逼紧 | 亚洲精品久久碰 | 羞羞私人影院可以直接免费观影吗 | dyav午夜片 | 青青国产成人久久91网 | 成年人在线免费看 | 欧洲老太玩小伙 | 黑人干亚洲人 | 搡60一70岁的老女人小说 | 国产玖玖在线观看 | 国产第一福利 | 奶茶视频官网免费 | 激情三级做爰在线观看激情 | 息与子中文字幕bd | 亚色九九九全国免费视频 | 热久久最新网址 | 好骚好紧 | 香蕉 在线播放 | 国产午夜成人无码免费看 | 视频大全在线观看免费 | 亚洲 在线 日韩 欧美 | 麻豆在线传煤 | 女仆色永久免费网站 | 欧美一区精品二区三区 | 欧美日韩国产在线人成dvd | 91九色在线视频 | 9久热久爱免费精品视频在线观看 | 欧美日韩精品乱国产 | 被夫上司强迫中文 | 成人小视频在线观看免费 | 免费xxxxx大片在线观看影视 | 调教老师肉色丝袜的故事 | 免费国产之a视频 | 久久免费国产视频 | 日韩一级生活片 | 韩剧在线观看 | 顶级欧美做受xxx000大乳 |