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








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







授权码模式
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







检验token
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

猜你喜欢

转载自huangyongxing310.iteye.com/blog/2383678