一.环境

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"
}

lines nums

学习Spring Security OAuth认证(一)-授权码模式的更多相关文章

  1. 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_06-SpringSecurityOauth2研究-Oauth2授权码模式-申请令牌

    3.3 Oauth2授权码模式 3.3.1 Oauth2授权模式 Oauth2有以下授权模式: 授权码模式(Authorization Code) 隐式授权模式(Implicit) 密码模式(Reso ...

  2. 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_07-SpringSecurityOauth2研究-Oauth2授权码模式-资源服务授权测试

    下面要完成  5.6两个步骤 3.3.4 资源服务授权 3.3.4.1 资源服务授权流程 资源服务拥有要访问的受保护资源,客户端携带令牌访问资源服务,如果令牌合法则可成功访问资源服务中的资 源,如下图 ...

  3. 单点登录 - OAuth 2.0 授权码模式(一)

    OAuth 2.0定义了四种授权方式 授权码模式(authorization code) 简化模式(implicit) 密码模式(resource owner password credentials ...

  4. Spring Security OAuth2 Demo —— 授权码模式

    本文可以转载,但请注明出处https://www.cnblogs.com/hellxz/p/oauth2_oauthcode_pattern.html 写在前边 在文章OAuth 2.0 概念及授权流 ...

  5. Oauth2.0认证---授权码模式

    目录: 1.功能描述 2.客户端的授权模式 3.授权模式认证流程 4.代码实现 1.功能描述 OAuth在"客户端"与"服务提供商"之间,设置了一个授权层(au ...

  6. OAuth 2.0之授权码模式

    转载自:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html OAuth 2.0授权码模式 授权码模式(authorization code)是功 ...

  7. Spring Security OAuth2 授权码模式

     背景: 由于业务实现中涉及到接入第三方系统(app接入有赞商城等),所以涉及到第三方系统需要获取用户信息(用户手机号.姓名等),为了保证用户信息的安全和接入方式的统一, 采用Oauth2四种模式之一 ...

  8. asp.net权限认证:OWIN实现OAuth 2.0 之授权码模式(Authorization Code)

    asp.net权限认证系列 asp.net权限认证:Forms认证 asp.net权限认证:HTTP基本认证(http basic) asp.net权限认证:Windows认证 asp.net权限认证 ...

  9. Spring Security OAuth笔记

    因为工作需要,系统权限安全方面可能要用到Spring Security OAuth2.0,所以,近几天了解了一下OAuth相关的东西.目前好像还没有系统的学习资料,学习主要是通过博客,内容都是大同小异 ...

随机推荐

  1. Writing device drivers in Linux: A brief tutorial

    “Do you pine for the nice days of Minix-1.1, when men were men and wrote their own device drivers?”  ...

  2. js操作css变量

    原文:http://css-live.ru/articles/dostup-k-css-peremennym-i-ix-izmenenie-spomoshhyu-javascript.html :ro ...

  3. 理解 Redis(5) - 哈希值

    哈希值存储示意图:首先, 我想先认真理解一下哈希值的数据结构:前面讲过, redis 存储的是键值对, 键永远都是可以打印的 ASCII 码, 值是字符串, 或者是以其他形式包裹的字符串. 上两节介绍 ...

  4. gcc 执行过程

    虽然我们称GCC是C语言的编译器,但使用gcc由C语言源代码文件生成可执行文件的过程不仅仅是编译的过程,而是要经历四个相互关联的步骤∶预处理(也称预编译,Preprocessing).编译(Compi ...

  5. 解决UnicodeEncodeError: 'gbk' codec can't encode character u'\u25aa' in position 344 : illegal multiby

    Python拿来做爬虫的确很不错,但是字符串的编码的确是稍不留神就是一个坑,GBK编码和Unicode编码的转化出现问题也是很多的,今天在解析网页数据的时候出现上述错误,解决方案如下: one_str ...

  6. CentOS7 下curl使用

    curl工具工具的主页:https://curl.haxx.se/NAMEcurl - transfer a URL SYNOPSIScurl [options] [URL...] DESCRIPTI ...

  7. python中字典的用法

    一,字典的简单介绍概念: 字典(dict)是python中唯一的一个映射类型.他是以{ }括起来的键值对组成. 在dict中key是 唯一的. 在保存的时候, 根据key来计算出一个内存地址. 然后将 ...

  8. 《剑指offer》第五十三题(0到n-1中缺失的数字)

    // 面试题53(二):0到n-1中缺失的数字 // 题目:一个长度为n-1的递增排序数组中的所有数字都是唯一的,并且每个数字 // 都在范围0到n-1之内.在范围0到n-1的n个数字中有且只有一个数 ...

  9. c# DataTable 序列化json

     if (ds.Tables[0].Rows.Count != 0)                 {                     var list = GetJsonString(ds ...

  10. array_map的使用

    其结果为: