spring oauth2.0

spring oauth2.0

grant_type :
authorization_code — 授权码模式(即先登录获取code,再获取token)
password — 密码模式(将用户名,密码传过去,直接获取token)
client_credentials — 客户端模式(无用户,用户向客户端注册,然后客户端以自己的名义向’服务端’获取资源)
implicit — 简化模式(在redirect_uri 的Hash传递token; Auth客户端运行在浏览器中,如JS,Flash)
refresh_token — 刷新access_token


JWT方式
就是token不用保存在服务器中,token里面包含有用户信息(没有密码)和权限信息,资源服务器要到授权服务器对比这个token的信息是否是正确的,通过校验签名来对比这是token的正确性。
当然授权和资源服务器的加密密匙要一致才能通过签名的一致辞性.


password — 密码模式
http://localhost:8080/oauth/token?grant_type=password&username=xing&password=123456


[img]http://dl2.iteye.com/upload/attachment/0126/7295/e5683ce3-1062-35b0-a562-3daa87b80882.png[/img]


[img]http://dl2.iteye.com/upload/attachment/0126/7297/55f44fd8-1e74-36b6-8267-48a695096543.png[/img]


客户端授权模式获取AccessToken
http://localhost:8080/oauth/token?grant_type=client_credentials


[img]http://dl2.iteye.com/upload/attachment/0126/7817/10319586-4189-3848-81f5-f31a19998825.png[/img]


授权码模式
http://localhost:8080/oauth/authorize?client_id=normal-app&response_type=code&scope=read&redirect_uri=http://localhost:8080/resources/user


localhost:8088/resources/user2?access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsic3ByaW5nLWJvb3QtYXBwbGljYXRpb24iXSwidXNlcl9uYW1lIjoieGluZyIsInNjb3BlIjpbInJlYWQiXSwicm9sZXMiOlt7ImF1dGhvcml0eSI6IlJPTEVfVVNFUiJ9XSwiZXhwIjoxNTAzOTk1ODI1LCJ1c2VyTmFtZSI6InhpbmciLCJhdXRob3JpdGllcyI6WyJST0xFX1VTRVIiXSwianRpIjoiOWU2ZjM2NmItYTI1Ni00N2FiLTk2YWUtMTU1M2RkYTZiN2M1IiwiY2xpZW50X2lkIjoibm9ybWFsLWFwcCJ9.qOI-x9Jhcr34UtyjQ-6JQY0qvD1VVDF8HNhuXUsTaTo


[img]http://dl2.iteye.com/upload/attachment/0126/7299/f03cef1b-a142-3807-a86a-348cd6f7868d.png[/img]


[b]检验token[/b]
http://localhost:8080/oauth/check_token?token=3f44c676-11eb-4c13-8cf3-b337f5079d33

跨服务器可用
http://localhost:8080/oauth/token?grant_type=password&username=xing&password=123456
http://localhost:9090/resources/user2?access_token=592dcf44-568f-419b-b24b-9c31bb9fae75


HttpSecurity

anonymous().disable() //匿名的

formLogin().permitAll()//允许所有用户访问这个页面

hasRole("USER")//有这个权限有才能访问
antMatchers("/").hasRole("USER")

authorizeRequests()//授权请求

authenticated()//要求在执行该请求时,必须已经登录了应用

.anyRequest().permitAll();//其他请求

csrf().disable() //CSRF攻击

.httpBasic() // 使用 Basic 认证

.antMatchers("/css/**", "/js/**", "/fonts/**", "/index").permitAll() // 都可以访问
.antMatchers("/h2-console/**").permitAll() // 都可以访问
.antMatchers("/users/**").hasRole("USER") // 需要相应的角色才能访问
.antMatchers("/admins/**").hasRole("ADMIN") // 需要相应的角色才能访问


@EnableAuthorizationServer
用户负责保证授权Endpoint(/oauth/authorize)的安全,但Token Endpoint(/oauth/token)将自动使用http basic的客户端凭证来保证安全

@EnableResourceServer
Oauth2 资源服务器的便利方法,开启了一个spring security的filter,这个filter通过一个Oauth2的token进行认证请求。
用者应该增加这个注解,并提供一个ResourceServerConfigurer类型的Bean(例如通过ResouceServerConfigurerAdapter)来指定资源(url路径和资源id)的细节。
@EnableResourceServer注解把一个 OAuth2AuthenticationProcessingFilter 类型过滤器添加到Spring Security 过滤链中。

ResourceServerConfiguration 和 SecurityConfiguration上配置的顺序, SecurityConfiguration一定要在ResourceServerConfiguration 之前,
因为spring实现安全是通过添加过滤器(Filter)来实现的,基本的安全过滤应该在oauth过滤之前, 所以在SecurityConfiguration设置@Order(2),
在ResourceServerConfiguration上设置@Order(6)


@EnableOAuth2Client


http.authorizeRequests().antMatchers(
"/swagger*/**"
, "/v2/api-docs/**"
, "/**/**" // 所有资源可以不登录访问
)
.permitAll();



(spring-security-oauth2注解详解):http://www.cnblogs.com/davidwang456/p/6480681.html
(OAuth 2.0 认证的原理与实践):http://blog.csdn.net/kkkloveyou/article/details/65531491
http://blog.csdn.net/u014453515/article/details/53406557
http://blog.csdn.net/zhoucheng05_13/article/details/60467234
http://blog.csdn.net/libaineu2004/article/details/38384487


配置多个认证模块时,只要一个符合就会通过的。前面的认证过了后面的就不会进行认证了.


参考:http://wwwcomy.iteye.com/blog/2230265
参考:http://www.oschina.net/translate/oauth-2-developers-guide
参考:http://lxgandlz.cn/403.html
参考:http://lxgandlz.cn/404.html
参考:http://andaily.com/spring-oauth-server/db_table_description.html
参考:http://blog.csdn.net/neosmith/article/details/52539927
参考:http://www.jfox.info/%E4%BB%8E%E9%9B%B6%E5%BC%80%E5%A7%8B%E7%9A%84springsecurityoauth2%E4%B8%80.html
参考:http://www.oschina.net/code/snippet_2429270_56647
参考:http://blog.csdn.net/neosmith/article/details/52539927
参考:http://blog.csdn.net/haiyan_qi/article/details/52384734

参考:https://github.com/niuyuzhou/staffManager
参考:http://www.leftso.com/blog/136.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jie310600

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值