Spring Boot starter for JustAuth Plus.

Overview

jap-spring-boot-starter

这是为JustAuth Plus 开发的Spring Boot Starter依赖。

访问https://github.com/Vector6662/jap-spring-boot-starter-demo ,为本starter的demo。

快速开始

Step 1:引入依赖

首先需要引入基本的maven依赖:

<dependency>
    <groupId>com.fujieid.jap.spring.bootgroupId>
    <artifactId>jap-spring-boot-starterartifactId>
    <version>${VERSION}version>
dependency>

为JustAuth Plus的四种授权策略 都提供了相应的starter:

jap-simple-spring-boot-starterjap-social-spring-boot-starterjap-oauth2-spring-boot-starterjap-oidc-spring-boot-starter

根据你的web后台需要支持的授权策略,引入相应的starter。如你的项目需要simple和oauth2授权方式,那么只需要在pom.xml中引入:

<dependency>
    <groupId>com.fujieid.jap.spring.bootgroupId>
    <artifactId>jap-simple-spring-boot-starterartifactId>
    <version>${VERSION}version>
dependency>
<dependency>
    <groupId>com.fujieid.jap.spring.bootgroupId>
    <artifactId>jap-oauth2-spring-boot-starterartifactId>
    <version>${VERSION}version>
dependency>

其他两种授权策略的maven坐标如下:

<dependency>
    <groupId>com.fujieid.jap.spring.bootgroupId>
    <artifactId>jap-social-spring-boot-starterartifactId>
    <version>${VERSION}version>
dependency>
<dependency>
    <groupId>com.fujieid.jap.spring.bootgroupId>
    <artifactId>jap-oidc-spring-boot-starterartifactId>
    <version>${VERSION}version>
dependency>

Step 2:application.properties中的基础配置

引入maven依赖后,你需要对jap-spring–boot-starter进行一些基础配置,多数情况下采用默认即可。下面是一些简单的例子。

# basic 基本配置
# 是否为sso
jap.basic.sso=false
jap.basic.cache-expire-time=12
jap.basic.token-expire-time=12
# 如果启启用了sso,则可能需要对sso进行一些配置
# sso
jap.sso.cookie-domain=xxx
jap.sso.cookie-max-age=xxx
jap.sso.cookie-name=xxx

准备工作已完成,接下来进行编码,以oauth2为例,有以下三个步骤:

Step 3:实现JapUserServiceType

为oauth2策略创建一个service,具体可以参考jap-oauth2:实现 JapUserService 接口特别注意的是,你需要在@Service注解中添加参数JapUserServiceType.OAUTH2,表明这是oauth2的JapUserService,像这样:

@Service(JapUserServiceType.OAUTH2)
public class Oauth2UserServiceImpl implements JapUserService {
	......
}

当然,也可以在application.properties中配置oauth2的JapUserService,即指定该service类的包全名:

jap.oauth2-user-service=my.dong6662.japspringbootstarterdemo.service.Oauth2UserServiceImpl

Step 4:application.properties中配置oauth2

oauth2提供了五种授权方式:授权码(authorization-code)隐式(implicit)密码(password)client-credentialsrefresh-token,选择一种或多种作为你的web应用支持的oauth2授权方式。这里提供了授权码 方式下的demo:

# gitee平台
# 授权码方式

# 指定平台
jap.oauth2[0].platform=gitee
# 授权码方式
jap.oauth2[0].grant-type=authorization_code
# 授权码方式下response-type必须为code
jap.oauth2[0].response-type=code
jap.oauth2[0].client-id=e9b4f19402d2ccb3375f5be19b9c76738fffe071d6b450a65dc4baa70a7ab752
jap.oauth2[0].client-secret=83bd48fc1ec9807f769c6328304e6222f2290b57d60f346a24976b48a752b794
# 你的应用服务器提供的接口,会接受code参数
jap.oauth2[0].callback-url=http://localhost:8080/oauth/gitee/authorization-code
# 获取token的地址
jap.oauth2[0].token-url=https://gitee.com/oauth/token
# 所有的api在:https://gitee.com/api/v5/swagger#/getV5User
jap.oauth2[0].userinfo-url=https://gitee.com/api/v5/user
# 获取user info的方法,GET、POST等。每个platform的不一样,需要查看具体平台的API
jap.oauth2[0].user-info-endpoint-method-type=get
# 获取授权码code的地址
jap.oauth2[0].authorization-url=https://gitee.com/oauth/authorize
jap.oauth2[0].verify-state=false

Step 5:编写Controller

使用JapTemplate,仅需在Controller中完成一行代码即可。调用authenticateByAuthorizationCode方法,表明使用的是授权码方式,并指定platform即可。

@RestController
@RequestMapping("/oauth")
public class Oauth2Controller {
    @Autowired
    JapTemplate japTemplate;
    
    @RequestMapping("/gitee/authorization-code")
    public JapResponse authorizationCode(){
        // 访问该路径需要有code参数
        return japTemplate.opsForOauth2().authenticateByAuthorizationCode("gitee");
    }
}

可以发现,调用authenticateByAuthorizationCode方法的url也是上边配置信息中jap.oauth2[0].callback-url填写的参数。

若你还想采用oauth2别的授权方式,比如密码,则可以调用方法:

public JapResponse authenticateByPassword(String platform, String username, String password)

但是别忘了在配置文件application.properties中添加密码方式相应的配置信息。

引入redis缓存

需要引入Spring Boot的redis starter:

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-data-redisartifactId>
    <version>${VERSION}version>
dependency>

并在application.properties中完成redis的基本配置

# redis配置
spring.redis.port=6379
spring.redis.host=127.0.0.1
spring.redis.timeout=3m

目前有两类信息需要缓存。

首先是token:

# token缓存
jap.cache.token.type=redis
jap.cache.token.expire-time=3m

其次,social策略有单独的缓存:

# social 缓存类型
jap.social.cache.type=redis

较为完整的配置

# basic 基本配置
jap.basic.sso=false
jap.basic.cache-expire-time=12
jap.basic.token-expire-time=12
# sso
jap.sso.cookie-domain=123
jap.sso.cookie-max-age=312321
jap.sso.cookie-name=3123124

# simple

# social

# social 缓存
jap.social.cache.type=default

jap.social.gitee.platform=gitee
jap.social.gitee.state=3242vregv
jap.social.gitee.just-auth-config.client-id=228d103043840b9706f04ad165726a0079c7e0263bf7c11f1205b4054ff094a9
jap.social.gitee.just-auth-config.client-secret=a06ccbdef86d193f25dc240d3e0a9038801ff3cf4c40937f2b58904c8f32a298
jap.social.gitee.just-auth-config.redirect-uri=http://localhost:8080/socail/gitee



# oauth2

# gitee平台
# 授权码方式
jap.oauth2[0].platform=gitee
jap.oauth2[0].response-type=code
jap.oauth2[0].client-id=e9b4f19402d2ccb3375f5be19b9c76738fffe071d6b450a65dc4baa70a7ab752
jap.oauth2[0].client-secret=83bd48fc1ec9807f769c6328304e6222f2290b57d60f346a24976b48a752b794
jap.oauth2[0].grant-type=authorization_code
# The URL in your application where users will be sent after authorization. 来自:https://docs.github.com/en/developers/apps/building-oauth-apps/authorizing-oauth-apps#parameters
# 用户授权后,也就是第一阶段gitee给我的服务器返回code的地址。这个参数我感觉我的服务器并没有啥用呀,感觉只有gitee端用得着。可能这是要作检查啥的吧,搞不懂。
# gitee服务器是不关心这个地址的有效性的,因为gitee只对这个地址进行重定向而不是转发,只不过让这个地址带上了参数code,也就是最终请求这个localhost地址的是用户的浏览器!
jap.oauth2[0].callback-url=http://localhost:8080/oauth/gitee/authorization-code
# 获取token的地址
jap.oauth2[0].token-url=https://gitee.com/oauth/token
# 所有的api在:https://gitee.com/api/v5/swagger#/getV5User
jap.oauth2[0].userinfo-url=https://gitee.com/api/v5/user
# 获取user info的方法,GET、POST等。每个platform的不一样,需要查看具体平台的API
jap.oauth2[0].user-info-endpoint-method-type=get
# 获取授权码code的地址
jap.oauth2[0].authorization-url=https://gitee.com/oauth/authorize
jap.oauth2[0].verify-state=false
# password方式
jap.oauth2[1].platform=gitee
jap.oauth2[1].client-id=e9b4f19402d2ccb3375f5be19b9c76738fffe071d6b450a65dc4baa70a7ab752
jap.oauth2[1].client-secret=83bd48fc1ec9807f769c6328304e6222f2290b57d60f346a24976b48a752b794
jap.oauth2[1].grant-type=password
jap.oauth2[1].token-url=https://gitee.com/oauth/token
jap.oauth2[1].callback-url=http://localhost:8080/oauth/gitee/redirect
jap.oauth2[1].userinfo-url=https://gitee.com/api/v5/user
jap.oauth2[1].user-info-endpoint-method-type=get


# GitHub平台
jap.oauth2[2].platform=github
jap.oauth2[2].response-type=code
jap.oauth2[2].client-id=772a23a61ae5ef9df25e
jap.oauth2[2].client-secret=d01a6a44bcf838d4e6d7b572279af59425e35a7a
jap.oauth2[2].grant-type=authorization_code
jap.oauth2[2].callback-url=http://localhost:8080/oauth/github/authorization-code
jap.oauth2[2].authorization-url=https://github.com/login/oauth/authorize
jap.oauth2[2].token-url=https://github.com/login/oauth/access_token
jap.oauth2[2].userinfo-url=https://api.github.com/user
jap.oauth2[2].verify-state=false

#微博
#参考api:https://open.weibo.com/wiki/%E5%BE%AE%E5%8D%9AAPI#OAuth2
jap.oauth2[3].platform=weibo
jap.oauth2[3].authorization-url=https://api.weibo.com/oauth2/authorize
# 这个地址在两个地方有用,第一个是访问/oauth2/authorize接口时作为重定向地址,第二个是访问/oauth2/access_token要带上,但是此时感觉没啥卵用,
# 但是微博就要求带上:回调地址,需需与注册应用里的回调地址一致。
jap.oauth2[3].callback-url=http://localhost:8080/oauth/weibo/authenticate-code
jap.oauth2[3].token-url=https://api.weibo.com/oauth2/access_token
jap.oauth2[3].access-token-endpoint-method-type=post
jap.oauth2[3].grant-type=authorization_code
jap.oauth2[3].response-type=code
jap.oauth2[3].client-id=xxx
jap.oauth2[3].client-secret=xxx
jap.oauth2[3].userinfo-url=https://api.weibo.com/2/users/show.json
jap.oauth2[3].user-info-endpoint-method-type=get
jap.oauth2[3].verify-state=true
jap.oauth2[3].state=245rfegfsaf
jap.oauth2[3].revoke-token-url=https://api.weibo.com/oauth2/revokeoauth2



# JapUserService
jap.simple-user-service=my.dong6662.japspringbootstarterdemo.service.SimpleUserServiceImpl
jap.social-user-service=my.dong6662.japspringbootstarterdemo.service.SocialUserServiceImpl
jap.oauth2-user-service=my.dong6662.japspringbootstarterdemo.service.Oauth2UserServiceImpl

# token缓存
jap.cache.token.type=redis
jap.cache.token.expire-time=3m

# redis配置
spring.redis.port=6379
spring.redis.host=127.0.0.1
spring.redis.timeout=3m
You might also like...

tuya-spring-boot-starter helps you efficiently create cloud development projects regarding the OpenAPI or message subscription capabilities. You can put all the focus on business logic without taking care of server-side programming nor relational databases.

English | 中文版 tuya-spring-boot-starter helps you efficiently create cloud development projects regarding the OpenAPI or message subscription capabilit

Dec 26, 2022

基于 spring-boot-starter-log4j2:2.6.1 (log4j 2.14.1)

Log4j 2 CVE-2021-44228 测试样本应用 基于 spring-boot-starter-log4j2:2.6.1 (log4j 2.14.1) 可用接口 接口 请求方法 参数 vulnerable_request_get GET v=payload vulnerable_reque

Mar 23, 2022

An awesome Spring Boot Starter!

 An awesome Spring Boot Starter!

spring-boot-tony-starter An awesome Spring Boot Starter! Explore the docs » View Demo · Report Bug · Request Feature Table of Contents About The Proje

Sep 13, 2022

Create your Java crypto trading bot in minutes. Our Spring boot starter takes care of exchange connections, accounts, orders, trades, and positions so you can focus on building your strategies.

Create your Java crypto trading bot in minutes. Our Spring boot starter takes care of exchange connections, accounts, orders, trades, and positions so you can focus on building your strategies.

Quick Start | Documentation | Discord | Twitter Create and run your java crypto trading bot in minutes Our Spring boot starter takes care of exchange

Jan 3, 2023

Get or Throw Spring boot Starter will help you to hide handling if entity not found.

Get or Throw Spring boot Starter Get or Throw Spring boot Starter will help you to hide handling if entity not found. 1. Setup 2. Usage Library adds c

Feb 2, 2022

Spring Boot Login and Registration example with MySQL, JWT, Rest Api - Spring Boot Spring Security Login example

Spring Boot Login and Registration example with MySQL, JWT, Rest Api - Spring Boot Spring Security Login example

Spring Boot Login example with Spring Security, MySQL and JWT Appropriate Flow for User Login and Registration with JWT Spring Boot Rest Api Architect

Jan 5, 2023

【多模块微服务脚手架平台——Ancba】前后端分离架构SpringBoot 2.x、SpringCloud、SpringAdmin、Spring Security、Mybatis-plus、(Shiro)、JWT、Feign、Nacos、Knif4j等。

【多模块微服务脚手架平台——Ancba】前后端分离架构SpringBoot 2.x、SpringCloud、SpringAdmin、Spring Security、Mybatis-plus、(Shiro)、JWT、Feign、Nacos、Knif4j等。

Ancba 打造Blog.Core项目的SpringBoot微服务版,但是更强大 👏 Ancba (Another New CLI By Alacrity) 另一个全新的敏捷脚手架(单体/模块化/微服务都可支持)。 核心知识点与进度 📣 在 ..../resources/application-

Nov 29, 2022

本项目基于springboot进行开发,实现了一系列的spring-boot-starter

项目简介 本项目基于springboot进行开发,实现了一系列的spring-boot-starter,可以作为开发中的工具包进行使用。 模块划分 common-spring-boot-starter:常用的基础类,比如用作消息流转的Msg以及一些工具类 monitor-spring-boot-st

Jan 24, 2022

about learning Spring Boot via examples. Spring Boot 教程、技术栈示例代码,快速简单上手教程。

about learning Spring Boot via examples. Spring Boot 教程、技术栈示例代码,快速简单上手教程。

Spring Boot 学习示例 Spring Boot 使用的各种示例,以最简单、最实用为标准,此开源项目中的每个示例都以最小依赖,最简单为标准,帮助初学者快速掌握 Spring Boot 各组件的使用。 Spring Boot 中文索引 | Spring Cloud学习示例代码 | Spring

Jan 1, 2023
Owner
Fujie
Just auth into any app
Fujie
Spring-Boot-Plus is a easy-to-use, high-speed, high-efficient,feature-rich, open source spring boot scaffolding

Everyone can develop projects independently, quickly and efficiently! What is spring-boot-plus? A easy-to-use, high-speed, high-efficient, feature-ric

geekidea 2.3k Dec 31, 2022
循序渐进,学习Spring Boot、Spring Boot & Shiro、Spring Batch、Spring Cloud、Spring Cloud Alibaba、Spring Security & Spring Security OAuth2,博客Spring系列源码:https://mrbird.cc

Spring 系列教程 该仓库为个人博客https://mrbird.cc中Spring系列源码,包含Spring Boot、Spring Boot & Shiro、Spring Cloud,Spring Boot & Spring Security & Spring Security OAuth2

mrbird 24.8k Jan 6, 2023
参考 DDD/Clean Architecture 设计理念,整合 Spring Boot/Spring Security/Mybatis Plus/Vavr 的 Spring Realworld 应用案例

Demo · 更多项目 · 参考资料 ms-spring-ddd-examples Unified Domain-driven Layered Architecture for MicroService Apps,试图探索一套切实可行的应用架构规范,可以复制、可以理解、可以落地、可以控制复杂性的指导

王下邀月熊 19 Sep 23, 2022
A springboot-starter that can achieve Intranet penetration. 一款可以实现内网穿透的springboot-starter。

qynat-springboot-starter 基于netty的内网穿透工具在springboot中的整合 protocol协议:protobuf 只需在application.properties中配置少量信息,实现零代码侵入的web项目内网穿透 项目的server端的源码在另一个多模块项目中,

whz11 65 Dec 12, 2022
A springboot-starter that can achieve Intranet penetration. 一款可以实现内网穿透的springboot-starter。

qynat-springboot-starter 基于netty的内网穿透工具在springboot中的整合 protocol协议:protobuf 只需在application.properties中配置少量信息,实现零代码侵入的web项目内网穿透 项目的server端的源码在另一个多模块项目中,

whz11 65 Dec 12, 2022
Kafka-spring-boot-starter: encapsulated based on spring-kafka

Encapsulation based on spring-kafka not only supports native configuration, but also adds multi data source configuration.

liudong 8 Jan 9, 2023
A web application to generate Java source code with spring-boot and mybatis-plus

A web application to generate Java source code with spring-boot and mybatis-plus. Also, The class of Domain,Mapper,XML of Mapper Interface,Service,Controller are included. You can change the data source what you want to generate for your project in app running without restart this code -generator application.

Weasley 3 Aug 29, 2022
Spring Boot starter module for gRPC framework.

Spring Boot starter module for gRPC framework.

Michael Zhang 2.8k Jan 4, 2023
Spring Boot starter module for gRPC framework.

Spring Boot starter module for gRPC framework.

Michael Zhang 1.8k Mar 17, 2021