侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

JSP 开发之Spring Security详解

Java  /  管理员 发布于 7年前   296

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="https:/article/admin.jsp" rel="external nofollow" >进入admin页面</a>   <a href="https:/article/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 的实例详解,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


  • 上一条:
    JSP 开发之Spring BeanUtils组件使用
    下一条:
    JSP 开发之Servlet解决网页缓存问题
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在java中实现的脱敏工具类代码示例分享(0个评论)
    • zookeeper安装流程步骤(0个评论)
    • 在java中你背的“八股文”可能已经过时了(2个评论)
    • 在php8.0+版本中使用属性来增加值代码示例(3个评论)
    • java 正则表达式基础,实例学习资料收集大全 原创(0个评论)
    • 近期文章
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-11
    • 2018-03
    • 2020-03
    • 2023-05
    • 2023-11
    • 2024-01
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客