update

parent cec4b450
spring.application.name=agent-oauth
spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false
eureka.client.enabled=false
eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://localhost:8761/eureka}
eureka.client.eureka-server-connect-timeout-seconds=5
eureka.client.eureka-server-read-timeout-second = 8
eureka.client.register-with-eureka=false
eureka.instance.preferIpAddress=true
eureka.client.fetch-registry=false
app.id=agent
apollo.meta=http://localhost:8280
apollo.bootstrap.enabled = true
apollo.bootstrap.namespaces = application,admin
apollo.cacheDir=/data/deploy/cache
shell.python.path=/usr/local/bin/python3
server.port=9073
wechat.mpAppId=wx2dda796c4029a99e
mpAppSecret=2d72283e65ec1c04d511703b285fdc34
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/opt/logs/resources-admin.log</file>
<append>true</append>
<encoder>
<pattern>%date [%thread] %level %logger{100}[%line] %msg%n</pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>/opt/logs/resources-admin.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<maxHistory>60</maxHistory>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
</appender>
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date [%thread] %level %logger{100}[%line] %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
<logger name="org.springframework.data" level="DEBUG" />
<logger name="org.springframework.cloud" level="INFO" />
<logger name="cn.benewtech.resources" level="DEBUG" />
<logger name="cn.benewtech.tower" level="DEBUG" />
<logger name="org.springframework.amqp" level="INFO" />
</configuration>
<?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>agent</artifactId>
<groupId>cn.benewtech</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>agent-oauth</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<exclusions>
<exclusion>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-apache-client4</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
</dependency>
<dependency>
<groupId>cn.benewtech</groupId>
<artifactId>tower-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<directory>${project.basedir}/target</directory>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
<resource>
<directory>config/${env}</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>cn.benewtech.agent.admin.AgentAdminApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
package cn.benewtech;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AgentOauthApplication {
public static void main(String[] args) {
SpringApplication.run(AgentOauthApplication.class, args);
}
}
package cn.benewtech.bo;
import lombok.Builder;
import lombok.Data;
import java.util.Date;
@Data
@Builder
public class AccessToken {
private Long userId;
private String token;
private Date expirationTime;
}
package cn.benewtech.config;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
/**
* 微信公众号配置
*/
@Component
public class WechatMpConfig {
@Autowired
private WechatMpProperties wechatMpProperties;
@Bean
private WxMpService wxMpService() {
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
return wxMpService;
}
@Bean
public WxMpConfigStorage wxMpConfigStorage() {
WxMpDefaultConfigImpl wxMpDefaultConfig = new WxMpDefaultConfigImpl();
wxMpDefaultConfig.setAppId(wechatMpProperties.getMpAppId());
wxMpDefaultConfig.setSecret(wechatMpProperties.getMpAppSecret());
return wxMpDefaultConfig;
}
}
package cn.benewtech.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatMpProperties {
private String mpAppId;
private String mpAppSecret;
}
package cn.benewtech.controller;
import cn.benewtech.dto.WechatUserDto;
import cn.benewtech.tower.beans.exception.BizException;
import cn.benewtech.tower.core.HttpResult;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.net.URLEncoder;
@Slf4j
@RestController
@RequestMapping("/wechat")
public class WechatController {
@Autowired
private WxMpService wxMpService;
/**
* 获取code参数
*
* @param authCallbackUrl
* @param returnUrl 需要跳转的url
* @return
*/
@GetMapping("/auth")
public HttpResult<String> authorize(@RequestParam String authCallbackUrl, @RequestParam String returnUrl) {
String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(authCallbackUrl,
WxConsts.OAuth2Scope.SNSAPI_USERINFO,
URLEncoder.encode(returnUrl));
log.info("【微信网页授权】获取code,redirectUrl = {}", redirectUrl);
return HttpResult.of(redirectUrl);
}
/**
* 初次授权获取用户信息
*
* @param code
* @param returnUrl
* @return
*/
@GetMapping("/auth/user/info")
public HttpResult<WechatUserDto> userInfo(@RequestParam("code") String code,
@RequestParam("state") String returnUrl) {
WxMpOAuth2AccessToken wxMpOAuth2AccessToken;
WxMpUser wxMpUser;
try {
wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
wxMpUser = wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null);
} catch (WxErrorException e) {
log.error("微信网页授权异常:{}", e);
throw new BizException("微信网页授权异常");
}
String openId = wxMpOAuth2AccessToken.getOpenId();
WechatUserDto wechatUser = new WechatUserDto(wxMpUser, wxMpOAuth2AccessToken.getAccessToken());
return HttpResult.of(wechatUser);
}
/**
* 根据openId和token获取用户信息(后面通过获取到的用户信息进行注册用户获取token)
* @param openId
* @param token
* @return
*/
@GetMapping("/token/user/info")
public HttpResult<WechatUserDto> getUserInfo(@RequestParam String openId, @RequestParam String token) {
WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken();
wxMpOAuth2AccessToken.setOpenId(openId);
wxMpOAuth2AccessToken.setAccessToken(token);
boolean ret = wxMpService.oauth2validateAccessToken(wxMpOAuth2AccessToken);
if (!ret) {
try {
// 刷新accessToken
wxMpOAuth2AccessToken = wxMpService.oauth2refreshAccessToken(wxMpOAuth2AccessToken.getRefreshToken());
} catch (WxErrorException e) {
log.error("【微信网页授权】刷新token失败,{}", e.getError().getErrorMsg());
throw new BizException(e.getError().getErrorMsg());
}
}
try {
WxMpUser wxMpUser = wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null);
WechatUserDto wechatUser = new WechatUserDto(wxMpUser, wxMpOAuth2AccessToken.getAccessToken());
return HttpResult.of(wechatUser);
} catch (WxErrorException e) {
log.error("【微信网页授权】获取用户信息失败,{}", e.getError().getErrorMsg());
throw new BizException(e.getError().getErrorMsg());
}
}
// public String getToken(User user) {
// String token="";
// token= JWT.create().withAudience(user.getId())
// .sign(Algorithm.HMAC256(user.getPassword()));
// return token;
// }
}
package cn.benewtech.dto;
import lombok.Data;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
@Data
public class WechatUserDto {
private String openid;
private String accessToken;
private String unionId;
private String nickname;
private String language;
private String country;
private String province;
private String city;
private Integer sex;
private String sexDesc;
private String headImgUrl;
public WechatUserDto(WxMpUser wxMpUser, String accessToken) {
this.setAccessToken(accessToken);
this.setOpenid(wxMpUser.getOpenId());
this.setUnionId(wxMpUser.getUnionId());
this.setNickname(wxMpUser.getNickname());
this.setLanguage(wxMpUser.getLanguage());
this.setCountry(wxMpUser.getCountry());
this.setProvince(wxMpUser.getCity());
this.setCity(wxMpUser.getCity());
this.setSex(wxMpUser.getSex());
this.setSexDesc(wxMpUser.getSexDesc());
this.setHeadImgUrl(wxMpUser.getHeadImgUrl());
}
}
package cn.benewtech.provider;
import cn.benewtech.bo.AccessToken;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
/**
* @version : 1.0
* @ClassName: JwtProvider
* @Description: TODO
* @Auther: caizp
* @Date: 2021-05-21 10:02
**/
@Slf4j
@Component
public class JwtProvider {
@Autowired
private JwtConfiguration jwtConfig;
public String getToken(HttpServletRequest request){
return request.getHeader(jwtConfig.getRequestHeader());
}
public AccessToken createToken(){
return null;
}
@Configuration
@Setter
@Getter
public static class JwtConfiguration{
/**
* 密匙
*/
private String apiSecretKey = "JWT_SECRET_KEY";
/**
* 过期时间-默认半个小时
*/
private Long expirationTime = 1800L;
/**
* 默认存放token的请求头
*/
private String requestHeader = "Authorization";
/**
* 默认token前缀
*/
private String tokenPrefix = "Bearer ";
}
}
spring.application.name=agent-oauth
spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false
eureka.client.enabled=false
eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://localhost:8761/eureka}
eureka.client.eureka-server-connect-timeout-seconds=5
eureka.client.eureka-server-read-timeout-second = 8
eureka.client.register-with-eureka=false
eureka.instance.preferIpAddress=true
eureka.client.fetch-registry=false
app.id=agent
apollo.meta=http://localhost:8280
apollo.bootstrap.enabled = true
apollo.bootstrap.namespaces = application,admin
apollo.cacheDir=/data/deploy/cache
shell.python.path=/usr/local/bin/python3
server.port=9073
wechat.mpAppId=wx2dda796c4029a99e
mpAppSecret=2d72283e65ec1c04d511703b285fdc34
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/opt/logs/resources-admin.log</file>
<append>true</append>
<encoder>
<pattern>%date [%thread] %level %logger{100}[%line] %msg%n</pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>/opt/logs/resources-admin.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<maxHistory>60</maxHistory>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
</appender>
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date [%thread] %level %logger{100}[%line] %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
<logger name="org.springframework.data" level="DEBUG" />
<logger name="org.springframework.cloud" level="INFO" />
<logger name="cn.benewtech.resources" level="DEBUG" />
<logger name="cn.benewtech.tower" level="DEBUG" />
<logger name="org.springframework.amqp" level="INFO" />
</configuration>
#Generated by Maven
#Wed Jun 02 15:20:09 CST 2021
version=1.0-SNAPSHOT
groupId=cn.benewtech
artifactId=agent-oauth
cn/benewtech/bo/AccessToken.class
cn/benewtech/config/WechatMpConfig.class
cn/benewtech/bo/AccessToken$AccessTokenBuilder.class
cn/benewtech/dto/WechatUserDto.class
cn/benewtech/controller/WechatController.class
cn/benewtech/config/WechatMpProperties.class
cn/benewtech/provider/JwtProvider$JwtConfiguration.class
cn/benewtech/provider/JwtProvider.class
/Users/caizp/Documents/BenewWorkSpace/benewspace01/agent/agent-oauth/src/main/java/cn/benewtech/provider/JwtProvider.java
/Users/caizp/Documents/BenewWorkSpace/benewspace01/agent/agent-oauth/src/main/java/cn/benewtech/controller/WechatController.java
/Users/caizp/Documents/BenewWorkSpace/benewspace01/agent/agent-oauth/src/main/java/cn/benewtech/bo/AccessToken.java
/Users/caizp/Documents/BenewWorkSpace/benewspace01/agent/agent-oauth/src/main/java/cn/benewtech/config/WechatMpProperties.java
/Users/caizp/Documents/BenewWorkSpace/benewspace01/agent/agent-oauth/src/main/java/cn/benewtech/AgentOauthApplication.java
/Users/caizp/Documents/BenewWorkSpace/benewspace01/agent/agent-oauth/src/main/java/cn/benewtech/dto/WechatUserDto.java
/Users/caizp/Documents/BenewWorkSpace/benewspace01/agent/agent-oauth/src/main/java/cn/benewtech/config/WechatMpConfig.java
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<modules> <modules>
<module>agent-admin</module> <module>agent-admin</module>
<module>agent-oauth</module>
<module>agent-repository</module> <module>agent-repository</module>
</modules> </modules>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment