学习Spring Security OAuth认证(一)-授权码模式
一.环境
spring boot+spring security+idea+maven+mybatis
主要是spring security
二.依赖
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
不需要版本号,spring boot的pom中已经声明了合适的版本号
三.springsecurity登录配置
package com.haitian.security; import com.haitian.service.security.CustomUserService;
import com.haitian.utils.PathUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.authentication.encoding.BasePasswordEncoder;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import java.util.Locale; /**
* User:zhangweixiao
* Description:
*/
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired
private AuthenticationFailureHandler authenticationFailureHandler;
@Autowired
private AuthenticationSuccessHandler authenticationSuccessHandler; @Autowired
private UserDetailsService userDetailsService; @Bean
public BasePasswordEncoder getPasswordEncoder()
{
return new Md5PasswordEncoder();
} @Bean
public DaoAuthenticationProvider authProvider() {
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
authProvider.setPasswordEncoder(getPasswordEncoder());
authProvider.setUserDetailsService(userDetailsService);
return authProvider;
} @Override
protected void configure(HttpSecurity http) throws Exception { ValidateCodeFilter validateCodeFilter=new ValidateCodeFilter();
validateCodeFilter.setAuthenticationFailureHandler(authenticationFailureHandler); http
.authorizeRequests()
.antMatchers("/**").permitAll()
.and()
.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class)
.formLogin().loginPage("/login").loginProcessingUrl("/zshop_login")
.failureHandler(authenticationFailureHandler)
.successHandler(authenticationSuccessHandler)
.and()
.csrf().disable();
} }
/**
* 前台登录的数据库桥梁
*/
@Service
public class CustomUserService implements UserDetailsService{ @Autowired
private UserService userService; /**
* @param username
* @return
* @throws UsernameNotFoundException
* @throws DataAccessException
*/
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException { User user = this.userService.getUserByUserName(username);
if (user == null) {
throw new UsernameNotFoundException("用户名不存在");
}
Set authorities = new HashSet();
for (Role role : user.getRoles()) {
GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(role.getRoleCode());
authorities.add(grantedAuthority);
}
user.setAuthorities(authorities);
return user;
} }
数据库中User表对应的User类继承了UserDetails,这里就不发了.
加密算法最好用bcrypt,因为数据库存的是md5的,所以先用md5了.
了解加密:http://www.cnblogs.com/ptqueen/p/8448396.html
配置完成之后要保证能登录成功.
四.配置认证服务器

1.配置注解提供认证服务器
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig {
}
2配置client-id和secret.
security:
oauth2:
client:
client-id: zshop
client-secret: zshop_secret
client-id就是第三方申请认证的id,前后端分离中前端也就相当于第三方.
你的认证服务器就好比QQ的认证服务器,你的前端就好比需要QQ第三方登录的网站.
3启动当前模块
可以看到控制台中oath2包的为我们做的mapping
Mapped "{[/oauth/authorize]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint.authorize(java.util.Map<java.lang.String, java.lang.Object>,java.util.Map<java.lang.String, java.lang.String>,org.springframework.web.bind.support.SessionStatus,java.
Mapped "{[/oauth/authorize],methods=[POST],params=[user_oauth_approval]}" onto public org.springframework.web.servlet.View org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint.approveOrDeny(java.util.Map<java.lang.String, java.lang.String>,java.util.Map<java.lang.String, ?>,org.springframework.web.bind.support.SessionStatus,java.security.Principal)
4.用rest client插件测试
关于插件简单介绍请看:
http://www.cnblogs.com/ptqueen/p/8449046.html
关于oath2文档
https://tools.ietf.org/html/rfc6749#page-24
上面是oath2文档中需要认证的参数信息
先这样测试,http://localhost:8082/oauth/authorize?response_type=code&client_id=zshop&redirect_uri=https://cnblogs.com&scope=all
五.获取授权码code

关于授权码模式的流程请看:
http://www.cnblogs.com/ptqueen/p/8449150.html
成功的话会跳转到登录页面,这个登录页面相当于从某个论坛QQ登录跳转出来的登陆窗口
登录成功后出现这样一个
OAuth Approval
Do you authorize 'zshop' to access your protected resources?
scope.all: Approve Deny
如果是自己的前端登录成功之后应该会直接拿到所有权限,不显示此页面,然后继续换取令牌,
第三方QQ登录的 approval是和登录页面一起的.后面看看怎么配置.
同意授权之后会跳转到https://cnblogs.com,同时会携带一个code,这个code就是授权码.下一步需要通过授权码来换取令牌.
https://www.cnblogs.com/?code=D120US
六.发送授权码到认证服务器换取令牌
参照oauth2文档
https://tools.ietf.org/html/rfc6749#page-29
post的参数有
grant_tpye,值必须为authorization_code
code,第一篇中获得的授权码
redirect_uri,必须和第一篇相同
client_id,配置文件中定义的client_id
scope,获取授权码时定义的scope
请求的header中需要包含client-id和client-secret.

七.获得令牌
成功之后即可获取,令牌是有有效期的,必须在有效期之前进行刷新
{"access_token": "f035137f-def1-4bae-9adc-718a26e6c141","token_type": "bearer","refresh_token": "e7f65ace-6c5d-44a6-94fb-e7ca65ff12fd","expires_in": 43199,"scope": "all"}
学习Spring Security OAuth认证(一)-授权码模式的更多相关文章
- 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_06-SpringSecurityOauth2研究-Oauth2授权码模式-申请令牌
3.3 Oauth2授权码模式 3.3.1 Oauth2授权模式 Oauth2有以下授权模式: 授权码模式(Authorization Code) 隐式授权模式(Implicit) 密码模式(Reso ...
- 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_07-SpringSecurityOauth2研究-Oauth2授权码模式-资源服务授权测试
下面要完成 5.6两个步骤 3.3.4 资源服务授权 3.3.4.1 资源服务授权流程 资源服务拥有要访问的受保护资源,客户端携带令牌访问资源服务,如果令牌合法则可成功访问资源服务中的资 源,如下图 ...
- 单点登录 - OAuth 2.0 授权码模式(一)
OAuth 2.0定义了四种授权方式 授权码模式(authorization code) 简化模式(implicit) 密码模式(resource owner password credentials ...
- Spring Security OAuth2 Demo —— 授权码模式
本文可以转载,但请注明出处https://www.cnblogs.com/hellxz/p/oauth2_oauthcode_pattern.html 写在前边 在文章OAuth 2.0 概念及授权流 ...
- Oauth2.0认证---授权码模式
目录: 1.功能描述 2.客户端的授权模式 3.授权模式认证流程 4.代码实现 1.功能描述 OAuth在"客户端"与"服务提供商"之间,设置了一个授权层(au ...
- OAuth 2.0之授权码模式
转载自:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html OAuth 2.0授权码模式 授权码模式(authorization code)是功 ...
- Spring Security OAuth2 授权码模式
背景: 由于业务实现中涉及到接入第三方系统(app接入有赞商城等),所以涉及到第三方系统需要获取用户信息(用户手机号.姓名等),为了保证用户信息的安全和接入方式的统一, 采用Oauth2四种模式之一 ...
- asp.net权限认证:OWIN实现OAuth 2.0 之授权码模式(Authorization Code)
asp.net权限认证系列 asp.net权限认证:Forms认证 asp.net权限认证:HTTP基本认证(http basic) asp.net权限认证:Windows认证 asp.net权限认证 ...
- Spring Security OAuth笔记
因为工作需要,系统权限安全方面可能要用到Spring Security OAuth2.0,所以,近几天了解了一下OAuth相关的东西.目前好像还没有系统的学习资料,学习主要是通过博客,内容都是大同小异 ...
随机推荐
- 提高R语言速度--转载
1. 参考<R语言编程艺术>(Norman Matloff) chapter 14 & chapter 15 2. 方法 (1)向量化 与非向量化-循环做个对比: ...
- html 进度条
<html> <head> <title>进度条</title> <style type="text/css"> .co ...
- strtotime 获取之前,之后时间
一年之前 <?php echo strtotime('-1 year'); ?> 一年之后 <?php echo strtotime('+1 year'); ?> 一月之前 & ...
- php CLI SAPI 内置Web Server
PHP 5.4.0起, CLI SAPI 提供了一个内置的Web服务器. 这个内置的Web服务器主要用于本地开发使用,不可用于线上产品环境. URI请求会被发送到PHP所在的的工作目录(Working ...
- Django与CSRF 、AJAX
CSRF(Cross-site request forgery)跨站请求伪造,是一种常见的网络攻击手段,具体内容和含义请大家自行百度. Django为我们提供了防范CSRF攻击的机制. 一.基本使用 ...
- EXpression 表达式目录树
表达式树 前面n-1的是一个表达式 最后一个是一个表达式 一直拆开拆到最后 继承ExpressionVisitor的类 可以重写获取到表达式树的方法进行扩张和改写 委托是编译成一个方法 表达 ...
- &&并且, ||或 , 的用法 ,区别
&&与运算必须同时都为true才是true,如果左边为false结果肯定为false: ||或运算,只要左边为true结果一定为true,两边都为false结果才是false. 只有当 ...
- npm i和npm install的区别
最近人用npm i来直接安装模块,但是有会报错,用npm install就不会报错,刚开始百思不得其解,它俩明明是同一个东西 后来查npm的帮助指令发现还是没区别,npm i仅仅是npm instal ...
- Android 通过 JNI 访问 Java 字段和方法调用
在前面的两篇文章中,介绍了 Android 通过 JNI 进行基础类型.字符串和数组的相关操作,并描述了 Java 和 Native 在类型和签名之间的转换关系. 有了之前那些基础,就可以实现 Jav ...
- Linux 安装SSH
●centOS/redhat安装SSH 查询openssh server服务状态:systemctl status sshd 安装sshd命令: yum install openssh-server ...