Spring oauth大致流程

Spring oauth大致流程,主要分为以下三步:

 

<1>登陆并授权,获取oauth_code

http://localhost:8081/api/oauth/authorize?client_id=1234&redirect_uri=http://localhost:8080/web/oauth/info&response_type=code&scope=read

 

forward:/oauth/confirm_access  --> /api/oauth/authorize

 

userApprovalHandler会来判断是否转到授权页面,这个Handler有几种实现:

ApprovalStoreUserApprovalHandler: 如果已存在有效的oauth_access_token,并且url中的scope是authentication中scope的子集,则无需授权

DefaultUserApprovalHandler:每次都需要授权

 

授权成功后会生成oauth_code,如果用的是jdbc方式,会存入数据库。

oauth_code不会过期

 

<2> 用oauth_code换取acceess_token

http://localhost:8081/api/oauth/token?client_id=1234&client_secret=1234&grant_type=authorization_code&code=CbF1Af&redirect_uri=http://localhost:8080/web/oauth/info

 

如果oauth_access_token有相应用户的记录,则获取原记录的access_token(在没过期的情况下)

成功换取access_code后,如果用jdbc方式,会存入oauth_access_token,并且删除oauth_code中对应的记录

如果需要开启refresh_token,需要在oauth_client_detail的authorized_grant_types中加入refresh_token

 

<3>资源访问

 http://localhost:8081/api/v1/user/identity/aaaa?access_token=8bd008e2-a2bf-4bfd-8cce-6a6ce4ccee0e

根据配置的voters来决定用户是否有访问资源的权限

如果有voter投了否决,则不能访问;

如果没有任何voter否决,且有voter同意,则能访问;

如果没有任何voter否决或同意,则根据配置的allowIfAllAbstainDecisions来决定是否允许访问

 

原生RoleVoter要验证authorization中的角色名称是否和资源的access角色相符

登陆时,如果对应用户在oauth_access_token中已存在有效数据(非过期),就会把authorization取出来,放到框架的context中去

需要扩展DefaultTokenServices 的createAccessToken方法,增加替换token机制,后续会塞进context,SecurityContextHolder.getContext().setAuthentication(userAuthentication)

原文地址:https://www.cnblogs.com/jerryshi1988/p/7249272.html