每个应用程序在存储用户信息时采取的策略是不同的,可以是数据库,LDAP目录,CAS认证等,SpringSecurity支
持的认证策略有:
(1)In-memory (Spring-configured) user repositories.
(2)JDBC-based user repositories.
(3)LDAP-based user repositories.
(4)OpenID decentralized user identity systems.
(5)Central Authentication System (CAS)
(6)X.509 certificates
(7)JAAS-based providers
当然也支持用户自定义。
下面的讲解以该配置为基础:
<http auto-config="true">
<intercept-url pattern="/**" access="ROLE_SPITTER" />
</http>
1. 配置基于内存的用户信息Repository
即在Spring配置文件中配置,使用<user-service>元素:
<user-service id="userService">
<user name
="habuma" password
="letmein" authorities
="ROLE_SPITTER,ROLE_ADMIN"/>
<user name="twoqubed" password="longhorns" authorities="ROLE_SPITTER"/>
<user name="admin" password="admin" authorities="ROLE_ADMIN"/>
</user-service>
使用<authentication-manager>元素注册认证管理器:
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userService"
/>
</authentication-manager>
通过这种方式是注册了一个ProviderManager,认证管理将认证工作委托给一个或多个认证Provider, 此时认证
Provider需要user service 提供User 信息。
与上面配置等价的内嵌式配置(不推荐
):
<authentication-provider>
<user-service id="userService">
<user name="habuma" password="letmein" authorities="ROLE_SPITTER,ROLE
_ADMIN"/>
...
</user-service>
</authentication-provider>
2. 配置基于数据库的用户信息Repository
即使用:<jdbc-user-service>元素,使用原理与user-service相同:
<jdbc-user-service id="userService" data-source-ref="dataSource" />
该配置使用data-source-ref属性指定数据源,通过该配置user service查找用户信息的SQL为:
select username,passwd,enabled from users where username = ?
查找用户已经授权的用户SQL:
select username,authority from authorities where username = ?
上面着两条SQL语句需要确保用户信息和授权正好存储在指定的表users和authorities中,而jdbc-user-
service提供了其他的属性用户客制化查找:
(1) users-by-username-query:根据条件用户名查找用户的:username,passwd和enabled状态;
(2) authorities-by-username-query: 根据条件用户名查找用户被授予的权限;
(3) group-authorities-by-username-query:根据用户名查找用户被授予的组权限。
示例:
<jdbc-user-service id="userService" data-source-ref="dataSource"
users-by-username-query
=
"select username, password, true from spitter where username=?"
authorities-by-username-query
=
"select username,'ROLE_SPITTER' from spitter where username=?" />
3. 配置基于的用户信息Repository
该配置适合于具有层级目录结构的授权体系。需要首先添加依赖:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-ldap</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
有两种配置方式:
(1) With an LDAP-oriented authentication provider
(2) With an LDAP-oriented user service
(1) 声明一个LDAP Authentication Provider
<authentication-manager alias="authenticationManager">
<ldap-authentication-provider user-search-filter="(uid={0})"
group-search-filter="member={0}"/>
</authentication-manager>
该配置中user-search-filter 和 group-search-filter属性用于提供一个过滤器,用与在基于LDAP查询用户和
组信息时,默认情况下该查找是从LDAP的Root开始查找,下面的配置可以指定query base:
<ldap-user-service id="userService"
user-search-base="ou=people"
user-search-filter="(uid={0})"
group-search-base="ou=groups"
group-search-filter="member={0}" />
该配置中指定查找用户的组织从"people"开始,查找组织从"groups"开始。
(2) 配置一个Passwdor Comparison
默认的进行LDAP认证策略是执行一个bind操作,直接与LDAP Server交互认证;另一种做法是执行一个
Comparison操作,这种方式是将密码发送至LDAP服务器,要求服务器进行密码比对,需要保证密码安全:
<ldap-authentication-provider
user-search-filter="(uid={0})"
group-search-filter="member={0}">
<password-compare />
</ldap-authentication-provider>
该配置默认需要登录表单中有userPassword属性,如果Password定义在不同的属性中,可配置成:
<password-compare hash="md5" password-attribute="passcode"
/>
配置中hash="md5"指定了密码的加密策略,默认支持的策略有:
{sha},{ssha},md4,md5,plaintext,sha,sha-256
(3) 声明一个LDAP User Service
<ldap-user-service id="userService"
user-search-base="ou=people"
user-search-filter="(uid={0})"
group-search-base="ou=groups"
group-search-filter="member={0}" />
(4)配置一个内嵌的LDAP Server
默认情况下,SpringSecurity LDAP 认证家丁LDAP Server监听的端口为33389 on localhost, 如果要自定义
在其他的机器上,需使用<ldap-server>元素:
<ldap-server url="ldap://habuma.com:389/dc=habuma,dc=com" />
内嵌的配置:
<ldap-server root="dc=habuma,dc=com" />
其中root是可选的,默认是"dc=springframework,dc=org",当然也可自己定义。
<ldap-server root="dc=habuma,dc=com" ldif="classpath:users.ldif" />
在上述这个配置中,将从classpath中查找所有LDIF文件,若要明确指定需使用ldif属性。
示例LDIF file:
dn: ou=groups,dc=habuma,dc=com
objectclass: top
objectclass: organizationalUnit
ou: groups
dn: ou=people,dc=habuma,dc=com
objectclass: top
objectclass: organizationalUnit
ou: people
dn: uid=habuma,ou=people,dc=habuma,dc=com
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: Craig Walls
sn: Walls
uid: habuma
userPassword: password
dn: uid=jsmith,ou=people,dc=habuma,dc=com
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: John Smith
sn: Smith
uid: jsmith
userPassword: password
dn: cn=spitter,ou=groups,dc=habuma,dc=com
objectclass: top
objectclass: groupOfNames
cn: spitter
member: uid=habuma,ou=people,dc=habuma,dc=com
4. 启用Remember-ME功能
需在<http>元素中使用<remember-me>元素:
<http auto-config="true" use-expressions="true">
...
<remember-me key="spitterKey" token-validity-seconds="2419200" />
</http>
如果不指定token-validity-seconds="2419200",默认情况下在cookie中存储有效信息2周,指定该信息后将
有效期改为4周。存储在Cookie中的数据包括username,password, expiration date,private key,这些数据
在存储前均已经被MD5编码,默认的可以是:SpringSecured",此出自定义为"spitterKey". 使用该功能需要在
Form 中增加属性:_spring_security_remember_me:
<input id="remember_me" name="_spring_security_remember_me
" type="checkbox"/>
<label for="remember_me" class="inline">Remember me</label>.
分享到:
相关推荐
aws身份验证secgroup脚本 使用(支持 ) ... -credentials :“ aws_account_id:aws_access_key” -url :使用模块 Api网关上部署的“ /连接” URL 样品用法 bash invoke_it.sh \ -method POST \ ...
authenticating user 1. 实现spring security的四种方法 1. 全部利用配置文件 不使用数据库,全部信息写在配置文件中,如拦截的URL及对应权限,指定用户名、密码和对应权限 2. 数据库+配置文件 数据库存储用户和权限...
Authenticating... Logged in successfully. >> Authentication token was received: 0.87fjt188jgntrzfr 1) List articles 2) List pages 3) Delete page 4) Delete article 5) New article 6) New page 7) Quit > ...
在Rails应用程序中对用户进行身份验证 基于对基础上, 在Pluralsight的免费一周结束后进行了跟进。 做了一些补充。 去做 测试没有运行(我认为最佳实践是在演示代码的同时演示测试)。 如果未输入无效的用户名,则...
# Terraform also supports authenticating via the Azure CLI too. # see here for more info: https://terraform.io/docs/providers/azuread/ # client_id = "..." # client_secret = "..." # tenant_id = "....
验证- 使用Passport验证Node.js应用程序
Docker映像中设置的默认CONFIG_FILE路径为/var/grgc/config.yaml 。用法$ docker run -v yourconfigpath.yaml:/var/grgc/config.yaml herlon214/gitlab-registry-...grgc Waiting for check period...grgc Found 3 im
github-package-test
PERM-GUARD: Authenticating the Validity of Flow Rules in Software Defined Networking
passport-wechat v2.0 strategy for authenticating with ##支持功能微信公众账号微信网站登陆安装$ npm install passport-wechat使用Configure Strategy passport.use(new WechatStrategy({ appID: {APPID}, name:{...
因为某些网络的不给力的原因,使用 Transporter 上传 ipa 到 App Store 时,有时间会卡住或者非常慢,一直停留在Authenticating with the App Store...可以使用这个文件替换下载很慢或者下载失败的Transporter 替换...
schemes for authenticating MPEG-4 streams: the Flat Authentication Scheme, the Progressive Authentication Scheme, and the Hierarchical Authentication Scheme. All the schemes allow authentication of ...
Then, you'll use Spring Security with the LDAP libraries for authenticating users and create a central authentication and authorization server using OAuth 2 protocol. Further, you'll understand how ...
// Authenticating Private channels.$ pusher -> socket_auth ( 'my-channel' , 'socket_id' );// Want to use the facade?Pusher :: get ( '/channels' );安装在项目的根目录中需要带有此软件包。 $ composer ...
FTX 美国 JavaScript 客户端 FTX.us的API客户端。 动机 ...// Retrieve secrets if authenticating const apiKey = process . env . FTX_API_KEY ; const apiSecret = process . env . FTX_API_SEC
balance-api-skanetrafiken Skånetrafiken ... * * @param { Object } options - The options object containing the credentials used * when authenticating against Skåntetrafiken. * @param { String|Number } o
Docker 身份验证和授权映像这是我在之前的博客文章中描述的微服务身份验证和授权模式的示例实现( 是模式, 是它如何扩展)。 它使用与和作为微服务,使用配置到容器中。 要求: JDK(本地编译java文件) (用于构建...
3.The Client Side:From Static to Dynamic Pages 4.Decoupled Drupal 5.Advantages of Decoupled Drupal 6.Disadvantages of Decoupled Drupal Partll.Decoupling Drupal 7.Decoupling Drupal 8 Core 8.Decoupling ...
spring cloud各子项目说明 1、eureka 目录为单机版的eureka demo 2、eureka-ha 三台机器组成的高可用集群 3、microservice-discovery-eureka-server-authenticating 基于HTTP basic证的Eureka Server 4、...
开源项目-davars-sohop.zip,sohop, an OAuth-authenticating reverse proxy