SSO-單點登陸(1)

   SSO-單點登陸(1)javascript

package com.sxt.sso.commons;

/**
 * 發送給客戶端的數據對象。
 * 商業開發中,通常除特殊請求外,大多數的響應數據都是一個統一類型的數據。
 * 統一數據有統一的處理方式。便於開發和維護。
 */
public class JWTResponseData {

    private Integer code;// 返回碼,相似HTTP響應碼。如:200成功,500服務器錯誤,404資源不存在等。
    
    private Object data;// 業務數據
    
    private String msg;// 返回描述
    
    private String token;// 身份標識, JWT生成的令牌。

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }
    
}
JWTResponseData
package com.sxt.sso.commons;

import io.jsonwebtoken.Claims;

/**
 * 結果對象。
 */
public class JWTResult {

    /**
     * 錯誤編碼。在JWTUtils中定義的常量。
     * 200爲正確
     */
    private int errCode;

    /**
     * 是否成功,表明結果的狀態。
     */
    private boolean success;

    /**
     * 驗證過程當中payload中的數據。
     */
    private Claims claims;

    public int getErrCode() {
        return errCode;
    }

    public void setErrCode(int errCode) {
        this.errCode = errCode;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public Claims getClaims() {
        return claims;
    }

    public void setClaims(Claims claims) {
        this.claims = claims;
    }
    
}
JWTResult
package com.sxt.sso.commons;

/**
 * 做爲Subject數據使用。也就是payload中保存的public claims
 * 其中不包含任何敏感數據
 * 開發中建議使用實體類型。或BO,DTO數據對象。
 */
public class JWTSubject {

    private String username;

    public JWTSubject() {
        super();
    }

    public JWTSubject(String username) {
        super();
        this.username = username;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    
}
JWTSubject
package com.sxt.sso.commons;

import java.util.HashMap;
import java.util.Map;

/**
 * 用於模擬用戶數據的。開發中應訪問數據庫驗證用戶。
 */
public class JWTUsers {

    private static final Map<String, String> USERS = new HashMap<>(16);
    
    static{
        for(int i = 0; i < 10; i++){
            USERS.put("admin"+i, "password"+1);
        }
    }
    
    // 是否可登陸
    public static boolean isLogin(String username, String password){
        if(null == username || username.trim().length() == 0){
            return false;
        }
        String obj = USERS.get(username);
        if(null == obj || !obj.equals(password)){
            return false;
        }
        
        return true;
    }
    
}
JWTUsers
package com.sxt.sso.commons;

import java.util.Date;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.SignatureException;

/**
 * JWT工具
 */
public class JWTUtils {
    
    // 服務器的key。用於作加解密的key數據。 若是可使用客戶端生成的key。當前定義的常亮能夠不使用。
    private static final String JWT_SECERT = "test_jwt_secert" ;
    private static final ObjectMapper MAPPER = new ObjectMapper();
    public static final int JWT_ERRCODE_EXPIRE = 1005;//Token過時
    public static final int JWT_ERRCODE_FAIL = 1006;//驗證不經過

    public static SecretKey generalKey() {
        try {
            // byte[] encodedKey = Base64.decode(JWT_SECERT); 
            // 無論哪一種方式最終獲得一個byte[]類型的key就行
            byte[] encodedKey = JWT_SECERT.getBytes("UTF-8");
            SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
            return key;
        } catch (Exception e) {
            e.printStackTrace();
             return null;
        }
    }
    /**
     * 簽發JWT,建立token的方法。
     * @param id  jwt的惟一身份標識,主要用來做爲一次性token,從而回避重放攻擊。
     * @param iss jwt簽發者
     * @param subject jwt所面向的用戶。payload中記錄的public claims。當前環境中就是用戶的登陸名。
     * @param ttlMillis 有效期,單位毫秒
     * @return token, token是一次性的。是爲一個用戶的有效登陸週期準備的一個token。用戶退出或超時,token失效。
     * @throws Exception
     */
    public static String createJWT(String id,String iss, String subject, long ttlMillis) {
        // 加密算法
        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
        // 當前時間。
        long nowMillis = System.currentTimeMillis();
        // 當前時間的日期對象。
        Date now = new Date(nowMillis);
        SecretKey secretKey = generalKey();
        // 建立JWT的構建器。 就是使用指定的信息和加密算法,生成Token的工具。
        JwtBuilder builder = Jwts.builder()
                .setId(id)  // 設置身份標誌。就是一個客戶端的惟一標記。 如:可使用用戶的主鍵,客戶端的IP,服務器生成的隨機數據。
                .setIssuer(iss)
                .setSubject(subject)
                .setIssuedAt(now) // token生成的時間。
                .signWith(signatureAlgorithm, secretKey); // 設定密匙和算法
        if (ttlMillis >= 0) { 
            long expMillis = nowMillis + ttlMillis;
            Date expDate = new Date(expMillis); // token的失效時間。
            builder.setExpiration(expDate);
        }
        return builder.compact(); // 生成token
    }
    
    /**
     * 驗證JWT
     * @param jwtStr
     * @return
     */
    public static JWTResult validateJWT(String jwtStr) {
        JWTResult checkResult = new JWTResult();
        Claims claims = null;
        try {
            claims = parseJWT(jwtStr);
            checkResult.setSuccess(true);
            checkResult.setClaims(claims);
        } catch (ExpiredJwtException e) { // token超時
            checkResult.setErrCode(JWT_ERRCODE_EXPIRE);
            checkResult.setSuccess(false);
        } catch (SignatureException e) { // 校驗失敗
            checkResult.setErrCode(JWT_ERRCODE_FAIL);
            checkResult.setSuccess(false);
        } catch (Exception e) {
            checkResult.setErrCode(JWT_ERRCODE_FAIL);
            checkResult.setSuccess(false);
        }
        return checkResult;
    }
    
    /**
     * 
     * 解析JWT字符串
     * @param jwt 就是服務器爲客戶端生成的簽名數據,就是token。
     * @return
     * @throws Exception
     */
    public static Claims parseJWT(String jwt) throws Exception {
        SecretKey secretKey = generalKey();
        return Jwts.parser()
            .setSigningKey(secretKey)
            .parseClaimsJws(jwt)
            .getBody(); // getBody獲取的就是token中記錄的payload數據。就是payload中保存的全部的claims。
    }
    
    /**
     * 生成subject信息
     * @param subObj - 要轉換的對象。
     * @return java對象->JSON字符串出錯時返回null
     */
    public static String generalSubject(Object subObj){
        try {
            return MAPPER.writeValueAsString(subObj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }
    
}
JWTUtils
package com.sxt.sso.controller;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sxt.sso.commons.JWTResponseData;
import com.sxt.sso.commons.JWTResult;
import com.sxt.sso.commons.JWTSubject;
import com.sxt.sso.commons.JWTUsers;
import com.sxt.sso.commons.JWTUtils;

@Controller
public class JWTController {

    @RequestMapping("/testAll")
    @ResponseBody
    public Object testAll(HttpServletRequest request){
        
        String token = request.getHeader("Authorization");
        JWTResult result = JWTUtils.validateJWT(token);
        
        JWTResponseData responseData = new JWTResponseData();
        
        if(result.isSuccess()){
            responseData.setCode(200);
            responseData.setData(result.getClaims().getSubject());
            // 從新生成token,就是爲了重置token的有效期。
            
            String newToken = JWTUtils.createJWT(result.getClaims().getId(), 
                    result.getClaims().getIssuer(), result.getClaims().getSubject(), 
                    1*60*1000);
            responseData.setToken(newToken);
            return responseData;
        }else{
            responseData.setCode(500);
            responseData.setMsg("用戶未登陸");
            return responseData;
        }
        
    }
    
    @RequestMapping("/login")
    @ResponseBody
    public Object login(String username, String password){
        JWTResponseData responseData = null;
        // 認證用戶信息。本案例中訪問靜態數據。
        if(JWTUsers.isLogin(username, password)){
            JWTSubject subject = new JWTSubject(username);
            String jwtToken = JWTUtils.createJWT(UUID.randomUUID().toString(), "sxt-test-jwt", 
                    JWTUtils.generalSubject(subject), 1*60*1000);
            responseData = new JWTResponseData();
            responseData.setCode(200);
            responseData.setData(null);
            responseData.setMsg("登陸成功");
            responseData.setToken(jwtToken);
        }else{
            responseData = new JWTResponseData();
            responseData.setCode(500);
            responseData.setData(null);
            responseData.setMsg("登陸失敗");
            responseData.setToken(null);
        }
        
        return responseData;
    }
    
}
JWTController
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.sxt.sso.controller" />
    
    <mvc:annotation-driven />
    
    <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>

</beans>
application.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>sso-cross-domain</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>charSetFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>charSetFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet>
    <servlet-name>mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>mvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>
web.xml
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">

function login(){
    var username = $("#username").val();
    var password = $("#password").val();
    var params = "username="+username+"&password="+password;
    $.ajax({
        'url' : '${pageContext.request.contextPath }/login',
        'data' : params,
        'success' : function(data){
            if(data.code == 200){
                var token = data.token;
                // web storage的查看 - 在瀏覽器的開發者面板中的application中查看。
                // local storage - 本地存儲的數據。 長期有效的。
                // session storage - 會話存儲的數據。 一次會話有效。
                var localStorage = window.localStorage; // 瀏覽器提供的存儲空間。 根據key-value存儲數據。
                localStorage.token = token;
            }else{
                alert(data.msg);
            }
        }
    });
}

function setHeader(xhr){ // XmlHttpRequest
    xhr.setRequestHeader("Authorization",window.localStorage.token);
}

function testLocalStorage(){
    $.ajax({
        'url' : '${pageContext.request.contextPath}/testAll',
        'success' : function(data){
            if(data.code == 200){
                window.localStorage.token = data.token;
                alert(data.data);
            }else{
                alert(data.msg);
            }
        },
        'beforeSend' : setHeader
    });
}

</script>
</head>
<body >
    <center>
        <table>
            <caption>登陸測試</caption>
            <tr>
                <td style="text-align: right; padding-right: 5px">
                登陸名:
                </td>
                <td style="text-align: left; padding-left: 5px">
                <input type="text" name="username" id="username"/>
                </td>
            </tr>
            <tr>
                <td style="text-align: right; padding-right: 5px">
                密碼:
                </td>
                <td style="text-align: left; padding-left: 5px">
                <input type="text" name="password" id="password"/>
                </td>
            </tr>
            <tr>
                <td style="text-align: right; padding-right: 5px" colspan="2">
                <input type="button" value="登陸" onclick="login();" />
                </td>
            </tr>
        </table>
    </center>
    <input type="button" value="testLocalStorage" onclick="testLocalStorage();"/>
</body>
</html>
index.jsp
<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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sxt</groupId>
    <artifactId>sso-jwt</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>
        <!-- JWT核心依賴 -->
        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>3.3.0</version>
        </dependency>
        <!-- java開發JWT的依賴jar包。 -->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.0</version>
        </dependency>
        <!-- 給springmvc提供的響應擴展。@ResponseBody -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <!-- <build>
        <pluginManagement>
            <plugins>
                配置Tomcat插件
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <configuration>
                    <port>80</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build> -->
</project>
pom.xml

 

http://localhost:8080/sso-jwt/login?username=admin1&password=password1html

 requestjava

  1. username:
    admin1
  2. password:
    password1

responsjquery

  {
    "code":200,
    "data":null,
    "msg":"登陸成功",
    "token":"eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJjOTY0NjAxMS04ZmU5LTQxZjItYmExYy0wM2Q4MjQ2ZTA4MjEiLCJpc3MiOiJzeHQtdGVzdC1qd3QiLCJzdWIiOiJ7XCJ1c2VybmFtZVwiOlwiYWRtaW4xXCJ9IiwiaWF0IjoxNTQwOTk0Mjc4LCJleHAiOjE1NDA5OTQzMzh9.9bIBYXjHiG1MXvhM8BQ_-pAY1Pk-46a0R2CtFOzYq6I"
}
web

 

2.   http://localhost:8080/sso-jwt/testAllajax

respons算法

{
    "code":200,
    "data":"{"username":"admin1"}",
    "msg":null,
    "token":"eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJjOTY0NjAxMS04ZmU5LTQxZjItYmExYy0wM2Q4MjQ2ZTA4MjEiLCJpc3MiOiJzeHQtdGVzdC1qd3QiLCJzdWIiOiJ7XCJ1c2VybmFtZVwiOlwiYWRtaW4xXCJ9IiwiaWF0IjoxNTQwOTk0MjgxLCJleHAiOjE1NDA5OTQzNDF9.h3-5CpljLTpd1f5STTv1jDUI4Y5k9AfLtZpgNairfIk"
}
spring

相關文章
相關標籤/搜索