package com.tuoheng.common; | |||||
import lombok.Data; | |||||
import org.springframework.beans.factory.annotation.Value; | |||||
import org.springframework.context.annotation.Configuration; | |||||
/** | |||||
* 2024年6月21日10:29:18 | |||||
* author: hanzhengxing | |||||
*/ | |||||
@Configuration | |||||
@Data | |||||
public class CommonConfig { | |||||
/** | |||||
* 机场地址 | |||||
*/ | |||||
public static String airportURL; | |||||
/** | |||||
* 机场url | |||||
* @param url | |||||
*/ | |||||
@Value("${tuoheng.airport-url}") | |||||
public void airportURL(String url) { | |||||
airportURL = url; | |||||
} | |||||
} |
.antMatchers("/toLogin", "/getHealth", "/static/**", "/vercode").permitAll() | .antMatchers("/toLogin", "/getHealth", "/static/**", "/vercode").permitAll() | ||||
.antMatchers("/user/create", "/user/getInfo").permitAll() | .antMatchers("/user/create", "/user/getInfo").permitAll() | ||||
.antMatchers("/third/authorize", "/third/redirect").permitAll() | .antMatchers("/third/authorize", "/third/redirect").permitAll() | ||||
.antMatchers("/third/test","/third/test1").permitAll() | |||||
.anyRequest().authenticated() | .anyRequest().authenticated() | ||||
) | ) | ||||
// Form login handles the redirect to the login page from the | // Form login handles the redirect to the login page from the |
package com.tuoheng.controller; | package com.tuoheng.controller; | ||||
import com.alibaba.druid.util.StringUtils; | |||||
import com.tuoheng.common.CommonConfig; | |||||
import com.tuoheng.service.ThirdService; | import com.tuoheng.service.ThirdService; | ||||
import com.tuoheng.until.JsonResult; | import com.tuoheng.until.JsonResult; | ||||
import lombok.extern.slf4j.Slf4j; | import lombok.extern.slf4j.Slf4j; | ||||
private ThirdService thirdService; | private ThirdService thirdService; | ||||
@GetMapping("/test") | |||||
public String test() { | |||||
return "finish..."; | |||||
} | |||||
@GetMapping("/test1") | |||||
public String test1() { | |||||
String url = CommonConfig.airportURL; | |||||
log.info(url); | |||||
return "finish..." + url; | |||||
} | |||||
/** | /** | ||||
* 通过clientId登录 | * 通过clientId登录 | ||||
* @param clientId | * @param clientId | ||||
* @return | * @return | ||||
*/ | */ | ||||
@GetMapping("/authorize") | @GetMapping("/authorize") | ||||
public JsonResult authorize(String clientId) { | |||||
return thirdService.authorize(clientId); | |||||
public JsonResult authorize(String clientId, String userName) { | |||||
return thirdService.authorize(clientId, userName); | |||||
} | } | ||||
/** | /** | ||||
*/ | */ | ||||
@CrossOrigin(origins = "https://airport.t-aaron.com") | @CrossOrigin(origins = "https://airport.t-aaron.com") | ||||
@GetMapping(value = "/redirect") | @GetMapping(value = "/redirect") | ||||
public void airportRedirect(HttpServletRequest req, HttpServletResponse resp) throws Exception { | |||||
log.info("访问airport"); | |||||
resp.sendRedirect("https://airport.t-aaron.com/transfer"); | |||||
public void airportRedirect(HttpServletRequest req, HttpServletResponse resp,String userName) throws Exception { | |||||
log.info("enter airportRedirect userName is {}", userName); | |||||
String url = CommonConfig.airportURL + "/transfer"; | |||||
if(!StringUtils.isEmpty(userName)){ | |||||
url += "?userName="+userName; | |||||
} | |||||
log.info("url is {}", url); | |||||
resp.sendRedirect(url); | |||||
} | } | ||||
/** | /** |
public interface ThirdService { | public interface ThirdService { | ||||
JsonResult authorize(String clientId); | |||||
JsonResult authorize(String clientId, String userName); | |||||
} | } |
package com.tuoheng.service.impl; | package com.tuoheng.service.impl; | ||||
import com.alibaba.druid.util.StringUtils; | |||||
import com.alibaba.fastjson.JSONObject; | import com.alibaba.fastjson.JSONObject; | ||||
import com.tuoheng.constants.OidcConstant; | import com.tuoheng.constants.OidcConstant; | ||||
import com.tuoheng.constants.ThirdConstant; | import com.tuoheng.constants.ThirdConstant; | ||||
import com.tuoheng.service.ThirdService; | import com.tuoheng.service.ThirdService; | ||||
import com.tuoheng.until.HttpUtils; | import com.tuoheng.until.HttpUtils; | ||||
import com.tuoheng.until.JsonResult; | import com.tuoheng.until.JsonResult; | ||||
import lombok.extern.slf4j.Slf4j; | |||||
import org.springframework.beans.BeanUtils; | import org.springframework.beans.BeanUtils; | ||||
import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||
import org.springframework.beans.factory.annotation.Value; | import org.springframework.beans.factory.annotation.Value; | ||||
import org.springframework.util.LinkedMultiValueMap; | import org.springframework.util.LinkedMultiValueMap; | ||||
import org.springframework.util.MultiValueMap; | import org.springframework.util.MultiValueMap; | ||||
import org.springframework.util.ObjectUtils; | import org.springframework.util.ObjectUtils; | ||||
import org.springframework.util.StringUtils; | |||||
import org.springframework.web.client.RestTemplate; | import org.springframework.web.client.RestTemplate; | ||||
import java.rmi.ServerException; | import java.rmi.ServerException; | ||||
* @Date 2023/11/11 10:19 | * @Date 2023/11/11 10:19 | ||||
*/ | */ | ||||
@Service | @Service | ||||
@Slf4j | |||||
public class ThirdServiceImpl implements ThirdService { | public class ThirdServiceImpl implements ThirdService { | ||||
* @return | * @return | ||||
*/ | */ | ||||
@Override | @Override | ||||
public JsonResult authorize(String clientId) { | |||||
public JsonResult authorize(String clientId, String userName) { | |||||
String username; | String username; | ||||
String password; | String password; | ||||
if(ThirdConstant.TUOHENG_AIRPORT_ADMIN.equals(clientId)){ | |||||
log.info("clientId is {}, userName is {}",clientId,userName); | |||||
if(ThirdConstant.TUOHENG_AIRPORT_ADMIN.equals(clientId) && !StringUtils.isEmpty(userName) && userName.equals("jnbigdata")){ | |||||
log.info("enterjnbigdata"); | |||||
username = "jnbigdata"; | |||||
password = "thjs2023"; | |||||
}else if(ThirdConstant.TUOHENG_AIRPORT_ADMIN.equals(clientId)){ | |||||
log.info("enterclientId clientId is " + ThirdConstant.TUOHENG_AIRPORT_ADMIN); | |||||
username = ThirdConstant.GZJJ_USERNAME; | username = ThirdConstant.GZJJ_USERNAME; | ||||
password = ThirdConstant.GZJJ_PASSWORD; | password = ThirdConstant.GZJJ_PASSWORD; | ||||
}else if(ThirdConstant.TUOHENG_TELECOMUMALE_ADMIN.equals(clientId)){ | }else if(ThirdConstant.TUOHENG_TELECOMUMALE_ADMIN.equals(clientId)){ | ||||
log.info("enterclientId clientId is " + ThirdConstant.TUOHENG_TELECOMUMALE_ADMIN); | |||||
username = ThirdConstant.HLCG_USERNAME; | username = ThirdConstant.HLCG_USERNAME; | ||||
password = ThirdConstant.HLCG_PASSWORD; | password = ThirdConstant.HLCG_PASSWORD; | ||||
}else { | }else { | ||||
return JsonResult.error("clientId error"); | return JsonResult.error("clientId error"); | ||||
} | } | ||||
log.info("userName is {},password is {}" , username,password); | |||||
//通过oidc的密码模式获取授权token 等相关信息数据 | //通过oidc的密码模式获取授权token 等相关信息数据 | ||||
TokenResult tokenResult = getToken(clientId,username,password); | TokenResult tokenResult = getToken(clientId,username,password); | ||||
tokenResult.setUserName(username); | tokenResult.setUserName(username); | ||||
headers.add("Authorization", "Basic " + authorization); | headers.add("Authorization", "Basic " + authorization); | ||||
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity(params, headers); | HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity(params, headers); | ||||
String jsonString = JSONObject.toJSONString(params); | |||||
String jsonString1 = JSONObject.toJSONString(headers); | |||||
log.info("sign json is " + jsonString); | |||||
log.info("sign json1 is " + jsonString1); | |||||
ResponseEntity<TokenResult> response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, parameterizedTypeReference); | ResponseEntity<TokenResult> response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, parameterizedTypeReference); | ||||
log.info("sign result is " + JSONObject.toJSONString(response)); | |||||
return response.getBody(); | return response.getBody(); | ||||
} | } | ||||
return JsonResult.success(userBaseInfoDto); | return JsonResult.success(userBaseInfoDto); | ||||
} | } | ||||
public static void main(String[] args) { | |||||
String str = new BCryptPasswordEncoder().encode("thjs2023"); | |||||
System.out.println(str); | |||||
} | |||||
} | } |
min-idle: 1 # 连接池中的最小空闲连接 | min-idle: 1 # 连接池中的最小空闲连接 | ||||
oauth2: | oauth2: | ||||
token: | token: | ||||
issuer: http://192.168.11.11:8090 | |||||
issuer: http://192.168.11.11:8090 | |||||
# 自定义配置 | |||||
tuoheng: | |||||
#airport配置地址 | |||||
airport-url: https://airport-test.t-aaron.com |
min-idle: 1 # 连接池中的最小空闲连接 | min-idle: 1 # 连接池中的最小空闲连接 | ||||
oauth2: | oauth2: | ||||
token: | token: | ||||
issuer: http://127.0.0.1:8090 | |||||
issuer: http://127.0.0.1:8090 | |||||
# 自定义配置 | |||||
tuoheng: | |||||
#airport配置地址 | |||||
airport-url: http://192.168.11.22:8060 |
min-idle: 1 # 连接池中的最小空闲连接 | min-idle: 1 # 连接池中的最小空闲连接 | ||||
oauth2: | oauth2: | ||||
token: | token: | ||||
issuer: https://oidc.t-aaron.com | |||||
issuer: https://oidc.t-aaron.com | |||||
tuoheng: | |||||
#airport配置地址 | |||||
airport-url: https://airport.t-aaron.com |
min-idle: 1 # 连接池中的最小空闲连接 | min-idle: 1 # 连接池中的最小空闲连接 | ||||
oauth2: | oauth2: | ||||
token: | token: | ||||
issuer: https://login-test.t-aaron.com | |||||
issuer: https://login-test.t-aaron.com | |||||
tuoheng: | |||||
#airport配置地址 | |||||
airport-url: https://airport-test.t-aaron.com |