我的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

    @Bean
public 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
@Override
protected AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManager();
} @Autowired
private MyAuthenticationProvider provider;//自定义验证 @Override
protected 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(); }
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
//将验证过程交给自定义验证工具
auth.authenticationProvider(provider);
} }

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

 /**
* 自定义验证方式
*/
@Override
public 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);
} @Override
public boolean supports(Class<?> arg0) {
return true;
}

这是ClientControlled 请求

@RestController
public class ClientControlled { @Autowired
private 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

Security-OAuth2.0 密码模式之客户端实现的更多相关文章

  1. oauth2.0密码模式详解

    oauth2.0密码模式 欢迎关注博主公众号「Java大师」, 专注于分享Java领域干货文章http://www.javaman.cn/sb2/oauth-password 如果你高度信任某个应用, ...

  2. Spring Security OAuth2 Demo —— 密码模式(Password)

    前情回顾 前几节分享了OAuth2的流程与授权码模式和隐式授权模式两种的Demo,我们了解到授权码模式是OAuth2四种模式流程最复杂模式,复杂程度由大至小:授权码模式 > 隐式授权模式 > ...

  3. IdentityServer4:IdentityServer4+API+Client+User实践OAuth2.0密码模式(2)

    一.密码模式实操 仍然使用第一节的代码:做如下改动: 1.授权服务端 前面我们使用项目:Practice.IdentityServer作为授权服务器 修改项目的Config.cs类: 添加测试用户,并 ...

  4. webapi之owin的oauth2.0密码模式_01概述

    一般在webapi接口中,为了防止接口被随意调用,都会验证用户身份. 然而不能每次调用接口都需要用户输入用户名密码来验证,这时就需要授权颁发令牌了,持有令牌就可以访问接口,接口也能验证令牌身份. 简单 ...

  5. springboot+spring security +oauth2.0 demo搭建(password模式)(认证授权端与资源服务端分离的形式)

    项目security_simple(认证授权项目) 1.新建springboot项目 这儿选择springboot版本我选择的是2.0.6 点击finish后完成项目的创建 2.引入maven依赖  ...

  6. 基于spring boot2.0+spring security +oauth2.0+ jwt微服务架构

    github地址:https://github.com/hankuikuide/microservice-spring-security-oauth2 项目介绍 该项目是一个演示项目,主要演示了,基于 ...

  7. Spring Cloud Security OAuth2.0 认证授权系列(一) 基础概念

    世界上最快的捷径,就是脚踏实地,本文已收录[架构技术专栏]关注这个喜欢分享的地方. 前序 最近想搞下基于Spring Cloud的认证授权平台,总体想法是可以对服务间授权,想做一个基于Agent 的无 ...

  8. Spring Security OAuth2.0认证授权二:搭建资源服务

    在上一篇文章[Spring Security OAuth2.0认证授权一:框架搭建和认证测试](https://www.cnblogs.com/kuangdaoyizhimei/p/14250374. ...

  9. Spring Security OAuth2.0认证授权四:分布式系统认证授权

    Spring Security OAuth2.0认证授权系列文章 Spring Security OAuth2.0认证授权一:框架搭建和认证测试 Spring Security OAuth2.0认证授 ...

随机推荐

  1. Linux centos下php安装cphalcon扩展的方法

    说明: 操作系统:CentOS php安装目录:/usr/local/php php.ini配置文件路径:/usr/local/php/etc/php.ini 1.安装cphalcon cd /usr ...

  2. 【poj3254】Corn Fields 状态压缩dp

    AC通道:http://vjudge.net/problem/POJ-3254 [题目大意] 农夫约翰购买了一处肥沃的矩形牧场,分成M*N(1<=M<=12; 1<=N<=12 ...

  3. JavaScript stringObject.replace() 方法

    定义和用法: replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 语法: stringObject.replace(RegExp/substr,reol ...

  4. [SoapUI] Mock Service

    https://www.soapui.org/getting-started/mock-services.html

  5. Solr分词搜索结果不准确

    Solr的schema.xml默认配置分词后条件取 OR 例如:大众1.6T  系统会自动分词为  [大众] [1.6T](ps:不同分词器分词效果不同)   会搜索出包含 [大众 OR  1.6T] ...

  6. SpringCloud之服务注册-eureka

    类似于DUBBO 的zookeeper, SpringCloud本身提供一套服务注册中心--eureka 与zookeeper的区别在于 1:zookeeper本身就是一个应用,安装即可用:eurek ...

  7. 【转】简说GNU, GCC and MinGW (Lu Hongling)

    原地址:https://my.oschina.net/u/588967/blog/73478 GNU, GCC, MinGW是开源社区常常要遇到的概念. 网上一般的解释比较繁琐, 让人如坠云雾. 本文 ...

  8. [GO]conext的使用

    package main import ( "context" "time" "net/http" "fmt" &quo ...

  9. [转]【流媒體】H264—MP4格式及在MP4文件中提取H264的SPS、PPS及码流

    [流媒體]H264—MP4格式及在MP4文件中提取H264的SPS.PPS及码流 SkySeraph Apr 1st 2012  Email:skyseraph00@163.com 一.MP4格式基本 ...

  10. ZOJ3775 ?(>_o)! 2017-04-13 23:37 110人阅读 评论(0) 收藏

    ?(>_o)! Time Limit: 2 Seconds      Memory Limit: 65536 KB ?(>_o)! is a pseudo-object-oriented ...