JSP 开发之Spring Security详解
jsp 开发之spring security详解
前言:
spring security是一个能够为基于spring的企业应用系统提供描述性安全访问控制解决方案的安全框架。它提供了一组可以在spring应用上下文中配置的bean,充分利用了spring ioc(依赖注入,也称控制反转)和aop(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。
spring security 的前身是 acegi security ,是 spring 项目组中用来提供安全认证服务的框架。spring security 为基于j2ee企业应用软件提供了全面安全服务。特别是使用领先的j2ee解决方案-spring框架开发的企业软件项目。
功能
spring security对web安全性的支持大量地依赖于servlet过滤器。这些过滤器拦截进入请求,并且在应用程序处理该请求之前进行某些安全处理。 spring security提供有若干个过滤器,它们能够拦截servlet请求,并将这些请求转给认证和访问决策管理器处理,从而增强安全性。根据自己的需要,可以使用表7.4中所列的几个过滤器来保护自己的应用程序。
如果使用过servlet过滤器,那么知道要让它们生效,就必须在web应用程序的web.xml文件中使用<filter> 和<filter-mapping>元素配置它们。虽然这样做能起作用,但是它并不适用于使用依赖注入进行的配置。
filtertobeanproxy是一个特殊的servlet过滤器,它本身做的工作并不多,而是将自己的工作委托给spring应用程序上下文 中的一个bean来完成。被委托的bean几乎和其他的servlet过滤器一样,实现javax.servlet.filter接 口,但它是在spring配置文件而不是web.xml文件中配置的。
实际上,filtertobeanproxy代理给的那个bean可以是javax.servlet.filter的任意实现。这可以是 spring security的任何一个过滤器,或者它可以是自己创建的一个过滤器。但是正如本书已经提到的那样,spring security要求至少配置四个而且可能一打或者更多的过滤器
通过在许多项目中实践应用以及社区的贡献,如今的spring security已经成为spring framework下最成熟的安全系统,它为我们提供了强大而灵活的企业级安全服务,如:
- 认证授权机制
- web资源访问控制
- 业务方法调用访问控制
- 领域对象访问控制access control list(acl)
- 单点登录(central authentication service)
- x509认证
- 信道安全(channel security)管理等功能
简单例子
1、创建web工程springsecurity3
2、把从spring网站下载的spring-security-3.1.0.release解压,并将其中的spring-security-samples-contacts-3.1.0.release.war解压,将jar包放到lib目录下。
3、修改配置web.xml如下:
<?xml version="1.0" encoding="utf-8"?> <web-app version="2.5" 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_2_5.xsd"> <!--加载spring xml配置文件 --> <context-param> <param-name>contextconfiglocation</param-name> <param-value> classpath:securityconfig.xml </param-value> </context-param> <!-- spring secutiry3.1的过滤器链配置 --> <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 容器启动监听器 --> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
4、在src下面创建securityconfig.xml文件内容如下:
<?xml version="1.0" encoding="utf-8"?> <b:beans xmlns="http://www.springframework.org/schema/security" xmlns:b="http://www.springframework.org/schema/beans" 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-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"> <!--登录页面不过滤 --> <http pattern="/login.jsp" security="none"/> <http access-denied-page="/accessdenied.jsp"> <form-login login-page="/login.jsp"/> <!--访问/admin.jsp资源的用户必须具有role_admin的权限 --> <intercept-url pattern="/admin.jsp" access="role_admin"/> <!--访问/**资源的用户必须具有role_user的权限 --> <intercept-url pattern="/**" access="role_user"/> <session-management> <concurrency-control max-sessions="1" error-if-maximum-exceeded="false"/> </session-management> </http> <authentication-manager> <authentication-provider> <user-service> <user name="john" password="john" authorities="role_user" /> <user name="admin" password="admin" authorities="role_user, role_admin" /> <user name="guest" password="guest" authorities="role_guest" /> </user-service> </authentication-provider> </authentication-manager> </b:beans>
5、在webroot中创建login.jsp内容如下:
<body> <form action="j_spring_security_check" method="post"> <table> <tr> <td>用户:</td> <td><input type='text'name='j_username'></td> </tr> <tr> <td>密码:</td> <td><input type='password'name='j_password'></td> </tr> <tr> <td><input name="reset"type="reset"></td> <td><input name="submit"type="submit"></td> </tr> </table> </form> </body>
6、在webroot中创建accessdenied.jsp,
<body> 您的访问被拒绝,无权访问该资源!<br> </body>
创建admin.jsp内容如下:
<body> 欢迎来到管理员页面. <br> </body>
修改index.jsp内容如下:
<body> 这是首页,欢迎<sec:authentication property="name"/>!<br> <a href="admin.jsp" rel="external nofollow" >进入admin页面</a> <a href="other.jsp" rel="external nofollow" >进入其它页面</a> </body>
好了,部署项目,并访问index.jsp.
用户名就是刚才部署的那个用户名。什么?忘了。那好吧,我再给你指出来
<user name="john" password="john" authorities="role_user" /> <user name="admin" password="admin" authorities="role_user, role_admin" />
权限不同访问的页面就不同。可以试试的
以上就是jsp 开发中spring security 的实例详解,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!