spring security helloworld级别

applicationContext-security.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 根目录下的login.jsp不需要springsecurity处理,任何用户都可以访问 -->
<http pattern="/login.jsp" security="none" />
<http pattern="/error.jsp" security="none" />

<!-- 需求:项目的所有资源都必须具备 ROLE_USER 角色才能访问 -->
<http auto-config="true">

<!-- 项目根目录下的admin.jsp必须具备ROLE_ADMIN角色才能访问 -->
<intercept-url pattern="/admin.jsp" access="ROLE_ADMIN"/>
<!-- 项目根目录下的所有资源都必须具备ROLE_USER角色的用户才能访问 -->
<intercept-url pattern="/**" access="ROLE_USER"/>


<!-- 自定义的登录页面 -->
<!--
login-page 指定自定义登录页面
default-target-url 指定登录成功跳转的页面
authentication-failure-url 指定登录失败跳转的页面
login-processing-url 指定登录处理路径,覆盖/j_spring_security_check,一般是一个虚拟路径
username-parameter 指定登录用户名接收字段,覆盖j_username
password-parameter 指定登录密码接收字段,覆盖j_password
-->
<form-login login-page="/login.jsp"
default-target-url="/index.jsp"
authentication-failure-url="/error.jsp"
login-processing-url="/doLogin.action"
username-parameter="username"
password-parameter="password"/>
</http>

<!-- 定义角色 -->
<authentication-manager>
<authentication-provider>
<user-service>
<user name="user" password="123456" authorities="ROLE_USER"/>
<user name="admin" password="654321" authorities="ROLE_ADMIN"/>
</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">

<!-- 添加spring支持 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>

<!-- 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 添加spring security3支持 -->
<!-- springSecurityFilterChain 在BeanIds类中 -->
<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>

</web-app>

备注:项目结构

 

posted @ 2016-02-25 02:27  ltang0  阅读(221)  评论(0编辑  收藏  举报