Commit b8cdb5ee zshaohui

1.登录提交

0 个父辈
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.neotel</groupId>
<artifactId>oneclickLogin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>oneclickLogin</name>
<description>oneclickLogin</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 导入配置文件处理器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.auth0/java-jwt -->
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.10.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.neotel.smf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SmfApplication {
public static void main(String[] args) {
SpringApplication.run(SmfApplication.class, args);
}
}
package com.neotel.smf.command.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonBean {
private String cid; //料仓名称
private String name; //料仓名字
private int value; //指定值
public String getOpKey(){
return cid + "X" + name;
}
@Override
public String toString() {
return "CommonBean{" +
"料仓id='" + cid + '\'' +
", 指令名称='" + name + '\'' +
", 指令值=" + value +
'}';
}
}
package com.neotel.smf.command.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultBean {
private int code;
private String msg;
private Object data;
public static ResultBean newOkResult(Object data){
return new ResultBean(0,"ok",data);
}
}
package com.neotel.smf.command.data;
import java.util.HashMap;
import java.util.Map;
public class DataCache {
private static Map<String,Integer> opMap = new HashMap<>();
public static void putOp(String key,Integer value){
opMap.put(key,value);
}
public static Integer getOpValue(String key){
if (opMap.get(key) != null){
Integer value = opMap.get(key);
opMap.remove(key);
return value;
}
return null;
}
}
package com.neotel.smf.command.rest;
import com.neotel.smf.command.bean.CommonBean;
import com.neotel.smf.command.bean.ResultBean;
import com.neotel.smf.command.data.DataCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("/smf/rest/api")
public class CommandController {
/**
* 接受指定
*
* @param commonBean
* @return
*/
@PostMapping("/opDeviceIo")
public ResultBean opDeviceIo(@RequestBody CommonBean commonBean) {
log.info("{}收到数据为:{}", "opDeviceIo", commonBean.toString());
DataCache.putOp(commonBean.getOpKey(), commonBean.getValue());
return ResultBean.newOkResult("");
}
/**
* 获取指定
*
* @param commonBean
* @return
*/
@PostMapping("/getStatus")
public ResultBean getStatus(@RequestBody CommonBean commonBean) {
log.info("{}收到数据为:{}", "getStatus", commonBean.toString());
Integer opValue = DataCache.getOpValue(commonBean.getOpKey());
log.info("{}获取到的指令为:{}", commonBean.getOpKey(), opValue);
return ResultBean.newOkResult(opValue);
}
}
package com.neotel.smf.login;
import com.alibaba.fastjson.JSON;
import com.neotel.smf.login.config.LoginConfiguration;
import com.neotel.smf.login.method.ParseJwtTokeMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.Map;
@Controller
@Slf4j
public class LoginController {
@Autowired
private LoginConfiguration loginConfiguration;
/**
* 登录页跳转
*
* @return
*/
@RequestMapping("/login")
public String login() {
return "login";
}
/**
* 登录成功页
*
* @return
*/
@RequestMapping("/loginSuccess")
public String loginSuccess() {
return "loginSuccess";
}
/**
* 确认登录页
*
* @return
*/
@RequestMapping("/confirm")
public String confirm() {
return "confirm";
}
@RequestMapping("/oauth2/authorize")
public String authorize(){
return confirm();
}
/**
* 一键登录重定向地址
*
* @return
*/
@RequestMapping("/loginRedirect")
@ResponseBody
public String loginRedirect() {
String redirectUrl = loginConfiguration.getAdfsBaseAddress()
+ "/oauth2/authorize"
+ "?"
+ "resource=" + loginConfiguration.getClientId()
+ "&client_id=" + loginConfiguration.getClientId()
+ "&response_type=code"
+ "&haschrome=1"
+ "&redirect_uri="+loginConfiguration.getRedirectUri()
+ "&code_challenge=jbxg31yfKmRhuBVutV-DtfoKsvEsDGKbDF_mEudl2a4"
+ "&code_challenge_method=S256"
+ "&state=15d57431-f939-4e4a-8006-fab1358cafee9507c217-d512-4f28-a8bf-c16928db80ce"
+ "&response_mode=form_post"
+ "&client-request-id=69ea5a4a-c0db-48c6-ad0e-feb9cee43301"
+ "&x-client-SKU=PCL.Desktop"
+ "&x-client-Ver=5.2.9.0"
+ "&x-client-CPU=x64"
+ "&x-client-OS=Microsoft+Windows+NT+6.2.9200.0";
log.info("一键登录重定向地址为:{}", redirectUrl);
return redirectUrl;
}
/**
* 确认登录
*
* @return
*/
@RequestMapping("/confirmLogin")
public ModelAndView confirmLogin(String token) {
log.info("方法{}请求参数为{}", "confirmLogin", token);
Map map = ParseJwtTokeMethod.parseJwtTokenMap(token);
ModelAndView modelAndView = new ModelAndView("redirect:" + loginConfiguration.getSuccessHtml());
modelAndView.addObject(JSON.toJSONString(map));
return modelAndView;
}
}
\ No newline at end of file
package com.neotel.smf.login.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "login")
@Data
public class LoginConfiguration {
private String adfsBaseAddress;
private String clientId;
private String redirectUri;
private String adfsMetadataAddress;
private String successHtml;
}
package com.neotel.smf.login.method;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import java.util.Base64;
import java.util.Map;
@Slf4j
public class ParseJwtTokeMethod {
/**
* 解析token的用户map信息
*
* @param token
* @return
*/
public static Map parseJwtTokenMap(String token) {
try {
if (Strings.isNotBlank(token)) {
token = token.substring(token.indexOf(".") + 1, token.lastIndexOf("."));
if (Strings.isNotBlank(token)) {
return JSON.parseObject(new String(Base64.getDecoder().decode(token)), Map.class);
}
}
} catch (Exception e) {
e.printStackTrace();
log.info("解析token的用户map信息异常", e);
}
return null;
}
}
server:
port: 8081
login:
#登录跳转地址
adfs-base-address: https://adfstest.micron.com/adfs
#客户端id
client-id: 4b3a662e-df9a-4215-8c11-19329d82cecb
#调用客户端重定向地址
redirect-uri: http://localhost/confirmLogin
adfs-metadata-address: https://adfstest.micron.com/federationmetadata/2007-06/federationmetadata.xml
#登录成功跳转页面
success-html: loginSuccess
spring:
thymeleaf:
prefix: classpath:/templates/
encoding: UTF-8
cache: false
suffix: .html
servlet:
content-type: text/html
\ No newline at end of file
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="UTF-8">
<title>confirmLogin</title>
</head>
<body>
<a href="/confirmLogin?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IkVQMWo2X0VkSktpSUM1SXhmbWRLbzNCZ0V3VSIsImtpZCI6IkVQMWo2X0VkSktpSUM1SXhmbWRLbzNCZ0V3VSJ9.eyJhdWQiOiJtaWNyb3NvZnQ6aWRlbnRpdHlzZXJ2ZXI6NGIzYTY2MmUtZGY5YS00MjE1LThjMTEtMTkzMjlkODJjZWNiIiwiaXNzIjoiaHR0cDovL2FkZnMubWljcm9uLmNvbS9hZGZzL3NlcnZpY2VzL3RydXN0IiwiaWF0IjoxNjU0MDc1OTcyLCJuYmYiOjE2NTQwNzU5NzIsImV4cCI6MTY1NDA3OTU3MiwiZW1haWwiOiJrbWxvd0BtaWNyb24uY29tIiwiUFVJRCI6Ijc5MjY5OCIsIndpbmFjY291bnRuYW1lIjoia21sb3ciLCJnaXZlbl9uYW1lIjoiS2F5IE1pbiIsImZhbWlseV9uYW1lIjoiTG93IiwiYXBwdHlwZSI6IlB1YmxpYyIsImFwcGlkIjoiNGIzYTY2MmUtZGY5YS00MjE1LThjMTEtMTkzMjlkODJjZWNiIiwiYXV0aG1ldGhvZCI6Imh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9hdXRoZW50aWNhdGlvbm1ldGhvZC93aW5kb3dzIiwiYXV0aF90aW1lIjoiMjAyMi0wNi0wMVQwOTozMjo1Mi40MDdaIiwidmVyIjoiMS4wIn0.rPXmludzCJhwhQh-dZUy2c_-HA3dMb5NA_1mdGoyWOkkMkY9Cj-t8EWSM4I44_cm0ICyuONQtf8KqEXm6VBL6KteOhgNGJOw0HKCAK2E8PMNnUArD1GGzhuKjByNXfrAdZI6A0-IvED3FTC6_SYXHC0fxibkCjxJ69UAKB1FvrAxeBpk_5KRtHee5aAKeDgy2LwF-kj7gNGaHMLUv2E8dndtUm7UrcdU2Y7Pnaakl5a9yVGzVbfaIsLG8OHr4uWeqv-rWFgkvQcZx2mp9KclwkV-dVDKYxY1tQmvGVoSt4d9dZ9kNBvas5i6jhEudH_unHF5oZKi92wh-rC0FSh8SA">
<button>confirmLogin</button>
</a>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
<script src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
</head>
<body>
<button id="oneClickLogin">login</button>
<script>
$(function() {
$('#oneClickLogin').on('click',function() {
$.ajax({
type: 'GET', //请求的方式
url: '/loginRedirect', // 请求的URL地址
success: function(res) { //请求成功之后的回调函数
window.open(res,'_blank')
}
})
})
})
</script>
</body>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>loginSuccess</title>
</head>
<body>
loginSuccess
<br>
<p th:text="${param. string } "></p>
</body>
</html>
\ No newline at end of file
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!