欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解Spring Security 简单配置

程序员文章站 2023-12-22 09:32:52
开发环境 maven idea jdk 1.8 tomcat 8 配置spring mvc + spring security pom....

开发环境

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

配置spring mvc + spring security

pom.xml

 <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

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

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

<?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会拦截所有请求,如果没有登录,则系统会跳转到spring security默认的登录页面。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: