Spring Security可以做的十件事

您可以在Spring XML配置文件中指定您选择的授权提供者。 您可以通过配置Spring的http://www.springframework.org/schema/security/spring-security-3.1.xsd模式中定义的authentication-manager来实现。 简化的authentication-manager元素定义看起来像这样:

<xs:element name='authentication-manager'><xs:complexType><xs:choice minOccurs='0' maxOccurs='unbounded'><xs:element name='authentication-provider'><xs:complexType><xs:choice minOccurs='0' maxOccurs='unbounded'><xs:element ref='security:any-user-service'/><xs:element name='password-encoder'>...</xs:element></xs:choice><xs:attributeGroup ref='security:ap.attlist'/></xs:complexType></xs:element><!-- This is BIG --><xs:element name='ldap-authentication-provider'>...</xs:element></xs:choice><xs:attributeGroup ref='security:authman.attlist'/></xs:complexType>
</xs:element>

这意味着,例如,您可以使用任何数量的身份验证提供程序,包括基本身份验证和JDBC身份验证,如下面的代码段所示:

<authentication-manager alias='authenticationManager'><authentication-provider><user-service><user authorities='ROLE_GUEST' name='guest' password=''/></user-service></authentication-provider><authentication-provider><jdbc-user-service data-source-ref='dataSource'/></authentication-provider></authentication-manager>


您可以使用intercept-url元素将URL链接到用户角色,从而在Spring XML文件中配置授权规则。 intercept-url元素是http元素的子元素,其简短定义如下所示:

<xs:element name='http'><xs:complexType><xs:choice minOccurs='0' maxOccurs='unbounded'><xs:element name='intercept-url'><xs:complexType><xs:attributeGroup ref='security:intercept-url.attlist'/></xs:complexType></xs:element><!-- Details omitted for clarity --><xs:element name='access-denied-handler'>...</xs:element><xs:element name='form-login'>...</xs:element><xs:element name='openid-login'>...</xs:element><xs:element name='x509'>...</xs:element><xs:element ref='security:jee'/><xs:element name='http-basic'>...</xs:element><xs:element name='logout'>...</xs:element><xs:element name='session-management'>...</xs:element><xs:element name='remember-me'>...</xs:element><xs:element name='anonymous'>...</xs:element><xs:element name='port-mappings'>...</xs:element><xs:element ref='security:custom-filter'/><xs:element ref='security:request-cache'/><xs:element name='expression-handler'>...</xs:element></xs:choice><xs:attributeGroup ref='security:http.attlist'/></xs:complexType>
</xs:element>

用法示例:

<security:http><security:intercept-url pattern='/admin/**' access='hasRole('ROLE_ADMIN')'/><security:intercept-url pattern='/account/**' access='hasRole('ROLE_USER')' /><security:intercept-url pattern='/**' access='hasRole('ROLE_ANONYMOUS')' /><!-- other elements removed for clarity -->
</security:http>


您可以使用几个实现Spring的org.springframework.security.authentication.encoding.PasswordEncoder接口的类对密码进行编码和验证。 这只有两种方法: encodePasswordisPasswordValid 。 它的许多实现包括:

  • BaseDigestPasswordEncoder
  • BasePasswordEncoder
  • LdapShaPasswordEncoder
  • Md4PasswordEncoder,
  • Md5PasswordEncoder
  • MessageDigestPasswordEncoder
  • MessageDigestPasswordEncoder
  • PlaintextPasswordEncoder
  • ShaPasswordEncoder


四个

您可以使用Spring Security的标签库来限制对页面元素的访问。 要使用此库,您需要在JSP中包含以下taglib定义:

<%@ taglib prefix='sec' uri='http://www.springframework.org/security/tags' %>

taglib包含三个有用的标签:

  • 授权
  • 认证方式
  • 访问控制表

有用的似乎是authorize标记,以Spring文档中的示例为例,可以以两种方式使用它。 首先,您可以授权角色:

<sec:authorize access='hasRole('supervisor')'>
This content will only be visible to users who have
the 'supervisor' authority in their list of <tt>GrantedAuthority</tt>s.
</sec:authorize>

…其次,您可以针对网址进行授权

<sec:authorize url='/admin'>
This content will only be visible to users who are authorized to send requests to the '/admin' URL.
</sec:authorize>

指定的URL必须与第2项中所述的intercept-url标记配合使用。

您可以使用Spring的内部注释执行方法级别的授权

  • @PreAuthorize('spEL expression')
  • @PostAuthorize('spEL expression')
  • @Secure

spEL表达式可以是任何东西,但通常类似于: hasRole('ROLE_USER')

要启用@PreAuthorize(...)@PostAuthorize(...)请将以下内容添加到XML配置文件中:

<global-method-security pre-post-annotations='enabled' />

如以下示例所示,使用@PreAuthorize(...)

<span class='java0'><br /></span><span class='java16'>@PreAuthorize</span><span class='java8'>(</span><span class='java5'>'hasRole('ROLE_ADMIN')'</span><span class='java8'>) <br /></span><span class='java4'>public </span><span class='java9'>void </span><span class='java10'>deleteUser</span><span class='java8'>(</span><span class='java10'>String username</span><span class='java8'>)</span><span class='java10'>;<br />
</span>

要启用@Secure请将以下内容添加到您的Spring配置文件中:

<global-method-security pre-post-annotations='enabled' />


您可以通过将以下内容添加到Spring配置文件中,使用Spring的JSR-250实现来执行方法级安全性:

<global-method-security jsr250-annotations=”enabled”/>

的JSR-250安全注解是一个子集的JSR-250注解和包括:

  • @RolesAllowed({“ROLE_USER”,”ROLE_ADMIN”})
  • @PermitAll
  • @DenyAll

使用时,JSR-250注释看起来像这样:

@RolesAllowed({"ROLE_ADMIN","ROLE_USER"})
public void deleteUser(String username);


您可以通过几个简单的步骤将Spring Security与OpenID身份验证集成。 其中的第一步是编写一个简单的JSP表单,其中将操作值设置为j_spring_openid_security_check ,该表单的最小值看起来像这样:

<form action='j_spring-openid-security-check' method='post'><label for='openid_idenifier'>Login</label>: <input id='openid_identifier' name='openid_identifier' type='text'/><input type='submit' value='Login' />
</form>

下一步是将openid-login元素添加到http

<xs:element name='http'><xs:complexType><xs:choice minOccurs='0' maxOccurs='unbounded'><xs:element name='openid-login'><xs:annotation><xs:documentation>Sets up form login for authentication with anOpen ID identity</xs:documentation></xs:annotation><xs:complexType><xs:sequence><xs:element minOccurs='0' maxOccurs='unbounded'ref='security:attribute-exchange' /></xs:sequence><xs:attributeGroup ref='security:form-login.attlist' /><xs:attribute name='user-service-ref' type='xs:token'><xs:annotation><xs:documentation>A reference to a user-service (orUserDetailsService bean) Id</xs:documentation></xs:annotation></xs:attribute></xs:complexType></xs:element><!-- Other elements omitted for clarity --></xs:choice></xs:complexType>
</xs:element>

由于所有openid-login子元素都是可选的,因此启用OpenID的最简单方法是编写:

<http auto-config='true'><openid-login/><!-- other tags and attributes omitted for clarity -->
</http>

最后,您需要将spring-security-openid.jar到您的项目中。

您可以将应用程序配置为使用XML配置通过嵌入式LDAP(轻型目录访问协议)服务器对用户进行身份验证。 下面显示的简化XML模式对此进行了描述:

<xs:element name='ldap-server'><xs:complexType><xs:attributeGroup ref='security:ldap-server.attlist' /></xs:complexType>
</xs:element>
<xs:attributeGroup name='ldap-server.attlist'><xs:attribute name='id' type='xs:token'><xs:annotation><xs:documentation>A bean identifier, used for referring to the bean elsewhere in the context.</xs:documentation></xs:annotation></xs:attribute><xs:attribute name='port' type='xs:positiveInteger'/><xs:attribute name='ldif' type='xs:string'><xs:annotation><xs:documentation>Explicitly specifies an ldif file resource to loadinto an embedded LDAPserver. The default is classpath*:*.ldiff</xs:documentation></xs:annotation></xs:attribute><xs:attribute name='root' type='xs:string'><xs:annotation><xs:documentation>Optional root suffix for the embedded LDAP server. Default is'dc=springframework,dc=org'</xs:documentation></xs:annotation></xs:attribute>
</xs:attributeGroup>

LDIF文件 (LDIF代表LDAP交换格式)是一种纯文本文件格式,用于描述一组LDAP记录。

ldap-server元素用法的一个示例是:

<ldap-server ldif='classpath:my-ldif-file.ldif' id='localserver' />

要使用Spring Security LDAP集成,请记住在项目的POM.XML中包含spring-security-ldap.jar jar。

您可以将应用程序配置为使用XML配置通过远程LDAP(轻型目录访问协议)服务器对用户进行身份验证。 下面显示的简化XML模式对此进行了描述:

<xs:element name='ldap-server'><xs:complexType><xs:attributeGroup ref='security:ldap-server.attlist' /></xs:complexType>
</xs:element>
<xs:attributeGroup name='ldap-server.attlist'><xs:attribute name='id' type='xs:token'><xs:annotation><xs:documentation>A bean identifier, used for referring to the bean elsewhere in the context.</xs:documentation></xs:annotation></xs:attribute><xs:attribute name='url' type='xs:token'/><xs:attribute name='port' type='xs:positiveInteger'/><xs:attribute name='manager-dn' type='xs:string'><xs:annotation><xs:documentation>Username (DN) of the 'manager' user identity which will be used toauthenticate to a (non-embedded) LDAP server. If omitted, anonymousaccess will be used.</xs:documentation></xs:annotation></xs:attribute><xs:attribute name='manager-password' type='xs:string'><xs:annotation><xs:documentation>The password for the manager DN. This is requiredif the manager-dn is specified.</xs:documentation></xs:annotation></xs:attribute>
</xs:attributeGroup>

文档指出ldap-server元素“定义LDAP服务器位置或启动嵌入式服务器。 URL指示远程服务器的位置。 如果未提供url,则将启动嵌入式服务器,监听提供的端口号。 该端口是可选的,默认为33389。将使用提供的ID为服务器注册Spring LDAP ContextSource bean。

这是一个非常简单的配置示例:

<ldap-server url='ldap://myServer/dc=captaindebug,dc=com:389' id='ldapExternal' manager-dn='uid=admin,ou=users,ou=systems' manager-password='s3cret'/>

配置服务器后,还需要配置LDAP身份验证提供程序。 似乎有几种方法可以做到这一点,但它并不是那么简单,所以以后可能会更多……

您可以添加
Spring Security Config的<intercept-url />元素requires-channel='https'属性强制所有匹配的URL使用HTTPS。 例如,如果要确保在发送密码之前始终对密码进行加密,则可以将此简化的XML添加到配置中:

<http auto-config='true' use-expressions='true'><intercept-url pattern='/login' requires-channel='https'/><!-- Other attributes and elements omitted -->    
</https>

这里还有另外几件事要做,但稍后会做更多……

您可能已经注意到,我已经使用Spring Security XML模式文件( http://www.springframework.org/schema/security/spring-security-3.1.xsd )来解释清单中的某些功能。可以使用Spring Security。 这是因为我一直将Spring XSD视为Spring所有事物的确定参考点。 2011年11月,我在Spring的JSR-250的@PostConstruct Annotation上写了一个博客,其中包含一个错误(是的,的确确实发生了),Spring的Chris Beams – @CBeams正确地指出了这一点,他在JavaLobby上发表了评论。此博客的版本 。 我决定检查架构,发现我们俩都错了(尽管我比克里斯错了很多)–就我所知,《 机长调试 》一书现在是正确的。

应用程序安全性是一个相当复杂的主题,如果您要深入研究它,那么我建议您获得Peter Mularien的Spring Security 3的副本- Spring的Guys也建议您这样做。

最后,如果有一个关于Spring Security的关键想法值得欣赏,那就是,作为应用程序的附加功能,它提供了非常丰富的安全功能集。 因此,您应该尝试让Spring Security尽可能多地处理应用程序的安全细节,而不是深入研究和不必要地编写自己的代码。

参考: Captain Debug's Blog博客上的JCG合作伙伴 Roger Hughes提供的Spring Security可以做的十件事 。

翻译自: https://www.javacodegeeks.com/2012/11/ten-things-you-can-do-with-spring-security.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/370334.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python编写自定义函数判断n1-n2范围内的素数_【每日道代码题001】- PYTHON基础复习...

问题001-1&#xff1a;请对输入三个整数a,b,c,判断能否以它们为三个边长构成三角形。若能&#xff0c;输出YES和面积&#xff0c;否则输出NOa float(input())b float(input())c float(input())if a > 0 and b > 0 and c > 0: #判断边长是否为正if (a b > c) an…

php绘制一个三角形,如何利用css或html5画出一个三角形?两种不同的制作三角形方法(代码实例)...

我们在平时的前端开发的时候&#xff0c;有时候是需要一些小图形来丰富一下页面效果&#xff0c;比如&#xff1a;下拉列表的倒三角图形。那么这样的一个三角形是如何制作出来的&#xff0c;本章给大家介绍如何利用css或html画出一个三角形&#xff1f;两种不同的制作三角形方法…

课时53.video标签(掌握)

这节课来学习一下html5中新增的标签&#xff0c;我们先来看一下&#xff0c;html5中新增了哪些标签&#xff1f; 打开W3school的网页&#xff0c;点击参考手册中的HTML/HTML5标签&#xff0c;有一个按字母顺序排列的标签&#xff0c;但凡标签后面带有5标记的&#xff0c;都是h…

Date函数基础知识整理

Date类型&#xff1a;1.Date.parse()接收一个表示日期的字符串参数&#xff0c;然后再根据这个字符串返回响应的日期的毫秒数&#xff1b;如&#xff1a;创建一个日期&#xff1a; 1 <script> 2 // var someDatenew Date(May 25,2004); 3 // console.log(someDate);//Tue…

Google Guava –与Monitor同步

Google Guava项目是每个Java开发人员都应该熟悉的库的集合。 Guava库涵盖I / O&#xff0c;集合&#xff0c;字符串操作和并发性。 在这篇文章中&#xff0c;我将介绍Monitor类。 Monitor是一种同步构造&#xff0c;可以在使用ReentrantLock的任何地方使用。 在任何时候&#x…

yaf 重写index.php,php框架Yaf路由重写实例代码

通常为了友好的URL格式&#xff0c;会进行站点URL的重写&#xff0c;可以在webserver(Nginx)的配置中进行rewrite&#xff0c;也可在在程序端进行&#xff0c;本文主要和大家介绍php框架Yaf路由重写&#xff0c;给大家做个参考&#xff0c;希望能帮助到大家。以下使用Yaf框架进…

python类初始化导入库_Python中optparser库用法实例详解

本文研究的主要是Python中optparser库的相关内容&#xff0c;具体如下。一直以来对optparser不是特别的理解&#xff0c;今天就狠下心&#xff0c;静下心研究了一下这个库。当然了&#xff0c;不敢说理解的很到位&#xff0c;但是足以应付正常的使用了。废话不多说&#xff0c;…

SQL--Chapter8--Working with Triggers and Transactions

Objectives:1.Implement triggers 2.Implement transactions 转载于:https://www.cnblogs.com/Catherinezhilin/p/7979644.html

Canvas制作的下雨动画

简介 在codepen上看到一个Canvas做的下雨效果动画&#xff0c;感觉蛮有意思的。就研究了下&#xff0c;这里来分享下&#xff0c;实现技巧。效果可以见下面的链接。 霓虹雨: http://codepen.io/natewiley/full/NNgqVJ/ 效果截图&#xff1a; Canvas动画基础 大家都知道&…

在Eclipse中有效使用JUnit

最近&#xff0c;我被卷入了讨论1和一些受感染的同伴2&#xff0c;他们关于我们如何在Eclipse IDE中使用JUnit 。 令人惊讶的是&#xff0c;对话带来了并非所有人都知道的一些“技巧”。 这使我有了写这篇文章的想法&#xff0c;总结了我们的演讲。 谁知道–也许有人也有新事物…

jquery文件上传控件 Uploadify

基于jquery的文件上传控件&#xff0c;支持ajax无刷新上传&#xff0c;多个文件同时上传&#xff0c;上传进行进度显示&#xff0c;删除已上传文件。 要求使用jquery1.4或以上版本&#xff0c;flash player 9.0.24以上。 有两个版本&#xff0c;一个用flash,一个是html5。html5…

imagick php 缩放,php使用imagick模块实现图片缩放、裁剪、压缩示例

PHP 使用Imagick模块 缩放&#xff0c;裁剪&#xff0c;压缩图片 包括gif图片缩放 裁剪代码如下:/*** 图片裁剪* 裁剪规则&#xff1a;* 1. 高度为空或为零 按宽度缩放 高度自适应* 2. 宽度为空或为零 按高度缩放 宽度自适应* 3. 宽度&#xff0c;高度到不为空或为…

php实现第三方邮箱登录_PHP实现用户异地登录提醒功能的方法

有时候你的网站账号被盗或你在别处登录操作后台时&#xff0c;右下角会弹出提示信息&#xff0c;提醒你的账号异地登录&#xff0c;或者会被强制下线。对于这种安全性要求比较高的web网站&#xff0c;很多后台管理都会做这种功能提醒。甄别自己的账号是否被盗或者是否有另一个人…

课时47.datalist标签(了解)

1.datalist标签 作用&#xff1a;给输入框绑定待选项 2.datalist格式&#xff1a; <datalist> <option>待选项内容</option> </datalist> 3.如何给输入框绑定待选列表&#xff1f; 搞一个输入框搞一个datalist列表给datalist列表标签添加一个id给…

pandas.read_csv参数详解

读取CSV&#xff08;逗号分割&#xff09;文件到DataFrame也支持文件的部分导入和选择迭代更多帮助参见&#xff1a;http://pandas.pydata.org/pandas-docs/stable/io.html参数&#xff1a;filepath_or_buffer : str&#xff0c;pathlib。str, pathlib.Path, py._path.local.Lo…

Gradle – Maven的观点

正如我博客的读者所知道的&#xff0c; 我有点像Maven迷 。 我从2007年8月左右开始使用Maven&#xff0c;从没有回过头。 但是&#xff0c;就像其他所有情况一样&#xff0c;“变化是唯一不变的”。 现在这个领域还有其他参与者&#xff0c;Gradle看起来是最有前途的。 我决定试…

postgis安装_从零开始,构建电子地图网站:0_2_数据处理postgis

软件安装完&#xff0c;开始数据处理。从China Historical GIS下载一份数据。一、数据下载数据来源&#xff1a;China Historical GIS&#xff1a;https://sites.fas.harvard.edu/~chgis/data/chgis/v6/先下载一份时间序列数据&#xff1a;Download CHGIS V6 TIME SERIES Datah…

sar图像去噪matlab,一种基于总曲率的SAR图像变分去噪方法与流程

本发明属于数字图像处理技术领域&#xff0c;具体涉及一种基于总曲率的SAR图像变分去噪方法。背景技术&#xff1a;&#xff1a;相干斑噪声是合成孔径雷达(Synthetic Aperture Radar&#xff0c;简称SAR)图像的重要特征&#xff0c;严重影响SAR图像的可解译性。相干斑噪声通常作…

Linux下用netstat查看网络状态、端口状态

在linux一般使用netstat 来查看系统端口使用情况步。 netstat命令是一个监控TCP/IP网络的非常有用的工具&#xff0c;它可以显示路由表、实际的网络连接以及每一个网络接口设备的 netstat命令的功能是显示网络连接、路由表和网络接口信息&#xff0c;可以让用户得知目…

课时2.浏览器和服务器(了解)

1.什么是浏览器&#xff1f; 浏览器就是由安装在我们电脑上的一款软件&#xff0c;QQ&#xff0c;百度影音等一样&#xff0c;都是安装在电脑上的一款软件 那这些软件之间由什么区别呢&#xff1f; 它们的区别就是它们的功能不太一样&#xff0c;QQ是用来聊天的&#xff0c;…