spring security详解

目录:

spring security的配置

二 UserDetailsService实现简单登陆

三 使用dataSource数据源进行登陆

四登陆后逻辑处理

五 remember-me 记住密码

六 权限层级控制

七 国际化错误信息

八 自定义过滤器,加入到securitychain

九 使用HTTPS


需要demo源码的请留下邮箱!

spring security的配置

首先,先把项目的整体结构以及整体配置贴出来,后面介绍中会将其中的功能模块一个一个的细讲解,稍安勿躁,一步一步的往下看:

本例使用springMVC+spring security进行测试,需要导入的jar包:

 

 

 

项目基本结构:

 

 

环境搭建,主要是三个配置文件:web.xml, applicationContext.xml, springmvc-security.xml 以及 springmvc-servlet.xml

1web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" 

xmlns="http://java.sun.com/xml/ns/javaee" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  <display-name></display-name>

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  

  <!-- 上下文配置文件路径 -->

  <context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

classpath:properties/applicationContext.xml,

   classpath:properties/springmvc-security.xml

      </param-value>

  </context-param>

  

  <!-- 上下文加载监听器 -->

  <listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

  <!-- 防止session溢出 -->

  <listener>

<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>

 </listener>

  

<!-- 中文过滤器 -->

<filter>

<filter-name>characterFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

<init-param>  

            <param-name>forceEncoding</param-name>  

            <param-value>true</param-value><!-- 强制进行转码 -->  

        </init-param>  

</filter>

<filter-mapping>

<filter-name>characterFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<!-- 然后接着是SpringSecurity必须的filter 优先配置,让SpringSecurity先加载,防止SpringSecurity拦截失效记住,filer的名字必须要用springSecurityFilterChain

-->  

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

  <!-- springmvcservlet -->

  <servlet>

<servlet-name>springmvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:properties/springmvc-servlet.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>springmvc</servlet-name>

<url-pattern>*.action</url-pattern>

</servlet-mapping>

</web-app>

 

二,applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans

xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans

 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

 http://www.springframework.org/schema/context 

     http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<!-- 加载配置文件 -->

<context:property-placeholder location="classpath:properties/jdbc.properties"/>

<!-- 扫描包,controller不扫描 -->

<context:component-scan base-package="com.springsecurity">

<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

</context:component-scan>

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"

init-method="init" destroy-method="close">

<property name="url" value="${druid.url}" />

<property name="username" value="${druid.username}" />

<property name="password" value="${druid.password}" />

<property name="maxActive" value="${druid.maxActive}" />

<property name="minIdle" value="${druid.minIdle}" />

<property name="maxWait" value="${druid.maxWait}" />

<property name="timeBetweenEvictionRunsMillis" value="${druid.timeBetweenEvictionRunsMillis}" />

<property name="minEvictableIdleTimeMillis" value="${druid.minEvictableIdleTimeMillis}" />

<property name="connectionProperties" value="config.decrypt=true" />

<property name="filters" value="config,stat,wall,log4j" />

</bean>

</beans>

 

三,springmvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

http://www.springframework.org/schema/mvc 

http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

http://www.springframework.org/schema/context 

http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<!-- 扫描包 -->

<context:component-scan base-package="com.springsecurity.action" />

 

<!-- 加载资源路径 -->

<mvc:resources location="/resources/" mapping="/resources/**" />

 

<mvc:annotation-driven/>

<!-- jsp页面视图处理 @author黄文韬 -->

<

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值