现在加最后一样,就是oauth2,现在很多网站都有对应的移动版本,那么移动端访问服务端的服务怎么控制权限,我知道的主要是两种方法,第一是模拟浏览器,访问服务的时候会生成session,之后在移动端缓存cookie,每次网络请求都把cookie加上,还有一种就是通过oauth2,登录之后生成一个凭证,每次请求时携带凭证,当然oauth2更多的是为第三方应用提供访问自己服务的权限。

oauth2的配置,可以纯配置文件打造,相比较前面的那些,可以说是最简单也是最复杂的,简单是因为引入jar包配置一个xml就可以,复杂是说这个仅有的xml需要写的东西很多理解起来也要费劲。

关于Oauth2的整合,很大程度上参考了这位仁兄的文章:http://blog.csdn.net/monkeyking1987/article/details/16828059

1. pom.xml

首先导入oauth2的包:

<properties>
……
<spring-security-oauth2.version>2.0.2.RELEASE</spring-security-oauth2.version>
</properties> <dependencies>
……
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>${spring-security-oauth2.version}</version>
</dependency>
……
</dependencies>

2. applicationContext-security.xml

oauth2是security的一部分,配置也有关联,就不再单建文件,首先要在最前面加一些schema:

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oauth2="http://www.springframework.org/schema/security/oauth2"

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/security/oauth2
http://www.springframework.org/schema/security/spring-security-oauth2-1.0.xsd

http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">

其实就是加了一个oauth2的命名。

在这个文件的开始,也就是之前配置的拦截链的http标签前面并列一个http标签:

<http pattern="/oauth/token" create-session="stateless" authentication-manager-ref="oauth2AuthenticationManager"
entry-point-ref="oauth2AuthenticationEntryPoint">
<intercept-url pattern="/oauth/token" access="IS_AUTHENTICATED_FULLY"/>
<anonymous enabled="false"/>
<http-basic entry-point-ref="oauth2AuthenticationEntryPoint"/>
<custom-filter ref="clientCredentialsTokenEndpointFilter" before="BASIC_AUTH_FILTER"/>
<access-denied-handler ref="oauth2AccessDeniedHandler"/>
</http>

这个标签处理/oauth/token的网络请求,这是oauth2的登录验证请求,那么登录需要什么,首先,和Spring Security一样,需要一个认证管理器,Spring Oauth2需要两个认证管理器,第一个就是之前Spring中配置的那一个,用来验证用户名密码的,还有一个是用来区分客户端用户的,给它起个名字叫oauth2AuthenticationManager:

<oauth2:client-details-service id="clientDetailsService">
<oauth2:client client-id="mobile_1" authorized-grant-types="password,authorization_code,refresh_token,implicit"
secret="secret_1"scope="read,write,trust" />
</oauth2:client-details-service>
<beans:bean id="oauth2ClientDetailsUserService"
class="org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService">
<beans:constructor-arg ref="clientDetailsService"/>
</beans:bean>
<authentication-manager id="oauth2AuthenticationManager">
<authentication-provider user-service-ref="oauth2ClientDetailsUserService"/>
</authentication-manager>

这儿设置了一种客户端,id叫做mobile_1,secret叫做secret_1,针对read、write和trust几个域有效。这几个域会在访问控制中被用到。

当登录成功之后会得到一个token,再次访问的时候需要携带这个token,spring-oauth2根据这个token来做认证,那么spring-oauth2必须先存一份token和用户关系的对应,因为不用session了,这就相当于session,那么这个token在服务器中怎么存,有两种主要的存储方式,一是创建数据表,把token存到数据库里,我现在追求简单可用,采用第二种方式,直接存到内存里。下面配置一个管理token的service:

<beans:bean id="tokenStore" class="org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore"/>
<beans:bean id="tokenServices" class="org.springframework.security.oauth2.provider.token.DefaultTokenServices">
<beans:property name="tokenStore" ref="tokenStore"/>
<beans:property name="supportRefreshToken" value="true"/>
</beans:bean>

下面配置4个基本的bean:分别处理访问成功、访问拒绝、认证点和访问控制:

<beans:bean id="oauth2AuthenticationEntryPoint"
class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"/> <beans:bean id="oauth2AccessDeniedHandler"
class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler"/>
<beans:bean id="oauthUserApprovalHandler"
class="org.springframework.security.oauth2.provider.approval.DefaultUserApprovalHandler"/> <beans:bean id="oauth2AccessDecisionManager" class="org.springframework.security.access.vote.UnanimousBased">
<beans:constructor-arg>
<beans:list>
<beans:bean class="org.springframework.security.oauth2.provider.vote.ScopeVoter"/>
<beans:bean class="org.springframework.security.access.vote.RoleVoter"/>
<beans:bean class="org.springframework.security.access.vote.AuthenticatedVoter"/>
</beans:list>
</beans:constructor-arg>
</beans:bean>

下一步,配置这个oauth2的server所能支持的请求类型:

<oauth2:authorization-server client-details-service-ref="clientDetailsService" token-services-ref="tokenServices"
user-approval-handler-ref="oauthUserApprovalHandler" >
<oauth2:authorization-code />
<oauth2:implicit />
<oauth2:refresh-token />
<oauth2:client-credentials />
<oauth2:password />
</oauth2:authorization-server>

比如说,如果配置本服务器不支持刷新token,那么就:

<oauth2:refresh-token disabled="true" />

我们的请求里,要把验证类型、用户名密码都作为表单参数提交,这就需要配置下面的filter:

<beans:bean id="clientCredentialsTokenEndpointFilter"
class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter">
<beans:property name="authenticationManager" ref="oauth2AuthenticationManager"/>
</beans:bean>

下面定义一种资源,指定spring要保护的资源,如果没有这个,访问控制的时候会说没有Authentication object:

<oauth2:resource-server id="mobileResourceServer"
resource-id="mobile-resource" token-services-ref="tokenServices" />

好了,到此为止基本配置就都有了,下面就看访问控制的配置:在前面的拦截链上,已经为登录验证配了一个/auth/token,在这个标签下面添加对/json和/admin这两个路径的控制(这里没有再写这两个访问的controller,依旧用前面几篇文章中写好的):

<http pattern="/json**" create-session="never"
entry-point-ref="oauth2AuthenticationEntryPoint"
access-decision-manager-ref="oauth2AccessDecisionManager">
<anonymous enabled="false" />
<intercept-url pattern="/json**" access="ROLE_USER" />
<custom-filter ref="mobileResourceServer" before="PRE_AUTH_FILTER" />
<access-denied-handler ref="oauth2AccessDeniedHandler" />
</http>
<http pattern="/admin**" create-session="never"
entry-point-ref="oauth2AuthenticationEntryPoint"
access-decision-manager-ref="oauth2AccessDecisionManager">
<anonymous enabled="false" />
<intercept-url pattern="/admin**" access="ROLE_ADMIN,SCOPE_READ" />
<custom-filter ref="mobileResourceServer" before="PRE_AUTH_FILTER" />
<access-denied-handler ref="oauth2AccessDeniedHandler" />
</http>

我们用oauth2AccessDecisionManager来做决策,这个地方需要注意,spring-security里面配置access="ROLE_USER,ROLE_ADMIN"是说user和admin都可以访问,是一个“或”的关系,但是这里是“与”的关系,比如第二个,需要ROLE_ADMIN并且当前的scope包含read才可以,否则就没有权限。认证失败会返回一段xml,这个可以自定义handler来修改,暂且按下不表。

源码下载

ps:使用方法:用户名密码前面spring-security中已经创建了用户,验证身份时访问:

http://localhost:8080/demo4ssh-security-oauth2/oauth/token?client_id=mobile_1&client_secret=secret_1&grant_type=password&username=zhangsan&password=123456

这时候会返回一个access_token:

{"access_token":"4219a91f-45d5-4a07-9e8e-3acbadd0c23e","token_type":"bearer","refresh_token":"d41df9fd-3d36-4a20-b0b7-1a1883c7439d","expires_in":43199,"scope":"read write trust"}

这之后再拿着这个access_token去访问资源:

http://localhost:8080/demo4ssh-security-oauth2/admin?access_token=4219a91f-45d5-4a07-9e8e-3acbadd0c23e

【JavaEE】SSH+Spring Security+Spring oauth2整合及example的更多相关文章

  1. Spring Security实现OAuth2.0授权服务 - 基础版

    一.OAuth2.0协议 1.OAuth2.0概述 OAuth2.0是一个关于授权的开放网络协议. 该协议在第三方应用与服务提供平台之间设置了一个授权层.第三方应用需要服务资源时,并不是直接使用用户帐 ...

  2. Spring Security 与 OAuth2 介绍

    个人 OAuth2 全部文章 Spring Security 与 OAuth2(介绍):https://www.jianshu.com/p/68f22f9a00ee Spring Security 与 ...

  3. 使用Spring Security和OAuth2实现RESTful服务安全认证

    这篇教程是展示如何设置一个OAuth2服务来保护REST资源. 源代码下载github. (https://github.com/iainporter/oauth2-provider)你能下载这个源码 ...

  4. Spring Security 与 OAuth2(介绍)

    https://www.jianshu.com/p/68f22f9a00ee Spring Security 与 OAuth2(介绍) 林塬 2018.01.23 11:14* 字数 3097 阅读 ...

  5. Spring Security实现OAuth2.0授权服务 - 进阶版

    <Spring Security实现OAuth2.0授权服务 - 基础版>介绍了如何使用Spring Security实现OAuth2.0授权和资源保护,但是使用的都是Spring Sec ...

  6. Spring Security基于Oauth2的SSO单点登录怎样做?一个注解搞定

    一.说明 单点登录顾名思义就是在多个应用系统中,只需要登录一次,就可以访问其他相互信任的应用系统,免除多次登录的烦恼.本文主要介绍 同域 和 跨域 两种不同场景单点登录的实现原理,并使用 Spring ...

  7. Spring Security教程之整合SpringMVC(六)

    一.前言 Spring Security系列教程中,前五篇为同一人所写,而本文是博主依据第三方文章整合而出,与前五篇文章的作者不是同一系列. 但本文以前五篇文章为基础,在前面文章所建立的Spring ...

  8. spring boot:spring security实现oauth2+jwt管理认证授权及oauth2返回结果格式化(spring boot 2.3.3)

    一,为什么oauth2要整合jwt? 1,OAuth2的token技术有一个最大的问题是不携带用户信息,所以资源服务器不能进行本地验证, 以致每次对于资源的访问,资源服务器都需要向认证服务器的toke ...

  9. Spring boot+Spring Security 4配置整合实例

    本例所覆盖的内容: 1. 使用Spring Security管理用户身份认证.登录退出 2. 用户密码加密及验证 3. 采用数据库的方式实现Spring Security的remember-me功能 ...

随机推荐

  1. twisted 学习笔记二:创建一个简单TCP客户端

    #coding=utf-8 from twisted.internet import reactor,protocol class QuickClient(protocol.Protocol): de ...

  2. sqlite3基础

    要使用sqlite,首先需要添加库文件libsqlite3.dylib.当你搜索libsqlite3关键字时,会发现还有一个libsqlite3.0.dylib的库文件,这里还是建议添加libsqli ...

  3. POJ 3320 Jessica's Reading Problem

    Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6001   Accept ...

  4. How to debug Typescript in browser

    How to debug typescript, In Chrome, we need to press F12, open settings, uncheck the Enable JavaScri ...

  5. 如何生成ipa文件

    xcode--Product--Archive 弹出Organizer-Archives选择Distribute---Save for EnterPrise or Ad_Hoc Deployment- ...

  6. 使用nodejs搭建图片服务器(一)

    背景 当我们开发一个Web项目的时候,为了将图片管理与web服务分离开,通常都会搭建一个图片服务器. 之所以选择nodejs是因为使用nodejs来搭建web项目相当简单而且快速,虽然这个图片服务器很 ...

  7. coreseek 提示 client version is higher than daemon version 解决办法

    安装好coreseek,开启了服务之后,通过 sphinx php扩展去请求数据,提示:client version is higher than daemon version (client is ...

  8. struts2整合CKEditor和CKFinder实现上传

    上一篇文章给大家分享了CKEditor+CKFinder+JSP实现了在线编辑器上传图片的功能,这里在给大家分享一下如何在前面的基础上在struts2下实现这样的功能. 实现与Struts2的整合,整 ...

  9. 再说 c++11 内存模型

    可见性与乱序 在说到内存模型相关的东西时,我们常常会说到两个名词:乱序与可见性,且两者经常交错着使用,容易给人错觉仿佛是两个不同的东西,其实不是这样,他们只是从不同的角度来描述一个事情,本质是相同的. ...

  10. Visual Studio 2010 简体中文旗舰、专业版(MSDN原版下载)

    Visual Studio 2010 简体中文旗舰.专业版(MSDN原版下载)(Visual Studio 2010 ultimate professional x86 dvd)2010[光盘镜像]- ...