我的OAuth2.0 客户端项目目录

pom 的配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>OauthText</artifactId><groupId>OauthText</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>OAuthClient</artifactId><dependencies><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></dependencies></project>

核心配置UlegalZCConfiger

    @Beanpublic OAuth2RestOperations restTemplate() {AccessTokenRequest atr = new DefaultAccessTokenRequest();OAuth2RestTemplate template = new OAuth2RestTemplate(resource(), new DefaultOAuth2ClientContext(atr));ResourceOwnerPasswordAccessTokenProvider provider = new ResourceOwnerPasswordAccessTokenProvider();template.setAccessTokenProvider(provider);return template;}private ResourceOwnerPasswordResourceDetails resource() {ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();resource.setClientId("000000");resource.setClientSecret("0000000");resource.setAccessTokenUri("http://192.168.100.1000:56/oauth/token");// Oauth2.0 服务端链接resource.setScope(Arrays.asList("read","write"));// 读写权限resource.setUsername("0000000");resource.setPassword("0000000");resource.setGrantType("password");// Oauth2.0 使用的模式 为密码模式return resource;}

上图username 和password 要与服务端自定义验证的账户和密码相同。setClientId和setClientSecret要与服务端数据库配置一样。如下字段

之后为前端拦截验证

package cn.xudy.sso.config;import cn.xudy.sso.Tool.MyAuthenticationProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;/*** Created by Joe on 2017/8/8.*/
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)//开启security注解
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Bean@Overrideprotected AuthenticationManager authenticationManager() throws Exception {return super.authenticationManager();}@Autowiredprivate MyAuthenticationProvider provider;//自定义验证
@Overrideprotected void configure(HttpSecurity http) throws Exception {// 全部通过
//        http.csrf().disable().authorizeRequests()
//                .anyRequest()
//                .permitAll();//允许所有用户访问"/"和"/home" 条件判断
        http.csrf().disable().authorizeRequests().antMatchers("/login", "/page-login.html").permitAll()//其他地址的访问均需验证权限.antMatchers("/*.html").authenticated().and().formLogin()//指定登录页是"/login".loginPage("/login").defaultSuccessUrl("/otherPage")//登录成功后默认跳转到"/index.html"
                .permitAll().and().logout().logoutUrl("/logout").logoutSuccessUrl("/login")//退出登录后的默认url是"/login".invalidateHttpSession(true).permitAll();}@Autowiredpublic void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {//将验证过程交给自定义验证工具
        auth.authenticationProvider(provider);}}

如果为条件验证,前端请求的话经过次方法,自定义验证代码WebSecurityConfig

 /*** 自定义验证方式*/@Overridepublic Authentication authenticate(Authentication authentication) throws AuthenticationException {String username = authentication.getName();String password = (String) authentication.getCredentials();System.out.println("=-=-=-=-=:"+username);// 假装请求数据库
User user=new User();Collection<? extends GrantedAuthority> authorities = AuthorityUtils.commaSeparatedStringToAuthorityList("USER");return new UsernamePasswordAuthenticationToken(user, password, authorities);}@Overridepublic boolean supports(Class<?> arg0) {return true;}

这是ClientControlled 请求

@RestController
public class ClientControlled {@Autowiredprivate OAuth2RestOperations oauthRestTemplate;@PostMapping(value = "/login")public String  saveCuringEvidence(@RequestBody User user ){System.out.println("---------------------Client"+user.getUsername());//  重点请求服务端oauthRestTemplate.postForEntity("http://192.168.1.100:9595/log",user,String.class);return user.getUsername();}}

最后建议先看看我写的服务端 两方配套使用

http://www.cnblogs.com/memoryXudy/p/7805178.html

转载于:https://www.cnblogs.com/memoryXudy/p/7805217.html

Security-OAuth2.0 密码模式之客户端实现相关推荐

  1. Spring Security Oauth2 之密码模式

    点击关注公众号,实用技术文章及时了解   作者:歪桃   blog.csdn.net/m0_37892044/article/details/113058924 前言,因为最近的项目是用Spring ...

  2. 实战讲解Spring Oauth2.0密码模式和授权码模式(内存inMemory+持久化jdbc配置)

    1 缘起 先吐槽, 在搜索关于Oauth2.0授权码方式认证时, 遇到的问题比较多,一句话,按照其分享的步骤一步一步来,最终,无法成功, 本想,抄近路,看一些前人分享的应用案例,直接使用, 近路不通, ...

  3. OAuth2.0授权协议与客户端授权码模式详解

    本文来重点讲解下OAuth2.0授权协议与客户端授权码模式 文章目录 什么是OAuth协议 交互过程 客户端授权模式 授权码模式 简化模式 密码模式 客户端模式 接入公司内部系统 后台管理系统 前台业 ...

  4. OAuth2.0 - 自定义模式授权 - 短信验证码登录

    一.OAuth2.0 - 自定义模式授权 上篇文章我们分析了目前的情况,演示了微服务的大环境下在保证安全的情况下通过SpringGateWay实现统一的鉴权处理,但是前面的演示中,我们都是基于用户名密 ...

  5. Spring Security OAuth2.0认证授权知识概括

    Spring Security OAuth2.0认证授权知识概括 安全框架基本概念 基于Session的认证方式 Spring Security简介 SpringSecurity详解 分布式系统认证方 ...

  6. Spring Security + OAuth2.0

    授权服务器 授权服务器中有4个端点.说明如下: Authorize Endpoint :授权端点,进行授权. Token Endpoint :令牌端点,经过授权拿到对应的Token. lntrospe ...

  7. Spring Security OAuth2 授权码模式 (Authorization Code)

    前言 Spring Security OAuth2 授权码模式 (Authorization Code) 应该是授权登录的一个行业标准 整体流程 首先在平台注册获取CLIENT_ID和CLIENT_S ...

  8. Spring Security OAuth2.0认证授权五:用户信息扩展到jwt

    历史文章 [Spring Security OAuth2.0认证授权一:框架搭建和认证测试] [Spring Security OAuth2.0认证授权二:搭建资源服务] [Spring Securi ...

  9. Spring Security Oauth2 授权码模式下 自定义登录、授权页面

    主要说明:基于若依springcloud微服务框架的2.1版本 嫌弃缩进不舒服的,直接访问我的博客站点: http://binarydance.top//aticle_view.html?aticle ...

最新文章

  1. Javascript cookie使用详解
  2. 在D-Bus适配器中声明信号
  3. Web最基本的弹出窗口代码(javascript)
  4. (转) Dockerfile 中的 COPY 与 ADD 命令 1
  5. 鼠标放到图片上替换图片,改变样式。
  6. putty mtputty 设置utf8编码
  7. docker部署showdoc
  8. STM32嵌入式基础开发04-PS2手柄SPI通讯数据输出(4_SPI)
  9. linux php验证码无法显示,PHPCMS在Linux下后台验证码无法显示的解决方法
  10. 才刚满30岁,就中年危机了...
  11. html5诊断报告,放射科诊断报告模板.docx
  12. 【VS2022引用其他项目的.cs文件】
  13. 【Arduino+ESP32专题】PlatformIO串口监视器的默认波特率修改
  14. 微信公众号开发(1)
  15. CodeBlocks中文汉化Code::Blocks 10.05 中文版
  16. lamdba表达式的使用
  17. notepad转换json_Notepad++的Json格式化插件
  18. css实现背景透明文字不透明
  19. 树莓派3b/3b+/、Pi(raspberry)摄像头安装
  20. C/C++编程解析PE文件结构

热门文章

  1. ERP成分简介--听觉感觉反应
  2. eeglab中文教程系列(8)-选择数据的epochs并进行比较
  3. [CES 2018] TPCast发布升级版本,将支持微软MR设备
  4. Win11让AMD很受伤:CPU游戏性能下降15%,官方建议暂时别升级
  5. 开发的AI程序员“抄”代码,被骂惨的GitHub到底冤不冤?
  6. 波士顿动力「全家」跳舞贺新年,马斯克点赞视频:这不是CG!
  7. 美国法官驳回特朗普工作签禁令:超出权职范围
  8. 微信也在用的Transformer加速推理工具 | 腾讯第100个对外开源项目
  9. 百度推ACE交通引擎:不仅是无人车,车路协同新基建我也包了
  10. 一文看尽CVPR 2019十大新研究:“不看也知”成热点,无人车新增重磅开源数据集...