Sfoglia il codice sorgente

1.登录功能
2.token验证
3.修改了柱状图无法切换数据源的bug

heqinwei 7 anni fa
parent
commit
5dad06e6f9

+ 5 - 14
pom.xml

@@ -56,20 +56,11 @@
       <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
 
-    <!-- dao-generator自动生成工具-->
-    <!--<dependency>-->
-      <!--<groupId>org.mybatis.generator</groupId>-->
-      <!--<artifactId>mybatis-generator-core</artifactId>-->
-      <!--<version>1.3.6</version>-->
-    <!--</dependency>-->
-
-    <!-- springboot 热加载 -->
-    <!--<dependency>-->
-      <!--<groupId>org.springframework.boot</groupId>-->
-      <!--<artifactId>spring-boot-devtools</artifactId>-->
-      <!--<optional>true</optional>-->
-      <!--&lt;!&ndash;<scope>true</scope>&ndash;&gt;-->
-    <!--</dependency>-->
+    <dependency>
+      <groupId>com.auth0</groupId>
+      <artifactId>java-jwt</artifactId>
+      <version>3.1.0</version>
+    </dependency>
 
     <!--dao -->
       <dependency>

+ 6 - 2
src/main/java/com/controller/ChartsConfigController.java

@@ -8,6 +8,7 @@ import com.server.ChartsConfigService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestHeader;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
@@ -22,6 +23,7 @@ public class ChartsConfigController {
    /*
    添加
     */
+//   @CheckToken
     @RequestMapping("/inputCharts")
     public RepEntity inputCharts(@RequestBody ChartConfigInfo body){
         return chartsConfigService.setChartsConfig(body);
@@ -54,16 +56,18 @@ public class ChartsConfigController {
     /*
     查询图表列表
      */
+    @CheckToken
     @RequestMapping("/getListCharts")
-    public RepEntity getListCharts(){
+    public RepEntity getListCharts(@RequestHeader String token){
         return chartsConfigService.getListCharts();
     }
 
     /*
     查询单个图表
      */
+    @CheckToken
     @RequestMapping("/getChartsConfig")
-    public RepEntity getChartsConfig(@RequestBody int body){
+    public RepEntity getChartsConfig(@RequestHeader String token,@RequestBody int body){
         return chartsConfigService.getOneCharts(body);
     }
 

+ 9 - 0
src/main/java/com/controller/CheckToken.java

@@ -0,0 +1,9 @@
+package com.controller;
+
+import java.lang.annotation.*;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+@Documented
+@interface CheckToken {
+}

+ 0 - 3
src/main/java/com/controller/ShowChartsController.java

@@ -1,11 +1,9 @@
 package com.controller;
 
-import com.model.vo.configVo.ColumnScreenInfo;
 import com.model.pojo.RepEntity;
 import com.model.vo.configVo.*;
 import com.server.*;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
@@ -33,7 +31,6 @@ public class ShowChartsController {
     展示柱状图
      */
     @RequestMapping("/showHistogram")
-    @Transactional
     public RepEntity showHistogram(@RequestBody HistogramConfigInfo body){
         return showHistogramService.showHistogram(body);
     }

+ 23 - 0
src/main/java/com/controller/UserController.java

@@ -0,0 +1,23 @@
+package com.controller;
+
+import com.model.pojo.RepEntity;
+import com.model.vo.configVo.LoginInfo;
+import com.server.UserService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+public class UserController {
+    @Autowired
+    UserService userService;
+
+    /*
+    登录(生成token)
+     */
+    @RequestMapping("/login")
+    public RepEntity login(@RequestBody LoginInfo body){
+        return userService.login(body);
+    }
+}

+ 21 - 0
src/main/java/com/dao/UserMapper.java

@@ -0,0 +1,21 @@
+package com.dao;
+
+import com.model.po.User;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
+import org.apache.ibatis.annotations.Update;
+import org.springframework.stereotype.Repository;
+
+@Mapper
+@Repository
+public interface UserMapper {
+
+    @Select("select BU_ID as id, BU_NAME as name, BU_DEPARTMENT as department, BU_POST as post, BU_ROLE as role," +
+            " user_name as userName, pass_word as passWord " +
+            "from bi_users where user_name = #{userName}")
+    User getLogin(String userName);
+
+    @Update("update bi_users set user_token = #{token} where bu_id = #{id}")
+    void updateToken(@Param("token") String token, @Param("id") int id);
+}

+ 22 - 0
src/main/java/com/model/po/TokenData.java

@@ -0,0 +1,22 @@
+package com.model.po;
+
+public class TokenData {
+    private String token;
+    private String times;
+
+    public String getToken() {
+        return token;
+    }
+
+    public void setToken(String token) {
+        this.token = token;
+    }
+
+    public String getTimes() {
+        return times;
+    }
+
+    public void setTimes(String times) {
+        this.times = times;
+    }
+}

+ 87 - 0
src/main/java/com/model/po/User.java

@@ -0,0 +1,87 @@
+package com.model.po;
+
+import java.util.Date;
+
+public class User {
+    private int id;
+    private String name;
+    private String userName;
+    private String passWord;
+    private String department;
+    private String post;
+    private String role;
+    private Date createDate;
+    private Date updateDate;
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getUserName() {
+        return userName;
+    }
+
+    public void setUserName(String userName) {
+        this.userName = userName;
+    }
+
+    public String getPassWord() {
+        return passWord;
+    }
+
+    public void setPassWord(String passWord) {
+        this.passWord = passWord;
+    }
+
+    public String getDepartment() {
+        return department;
+    }
+
+    public void setDepartment(String department) {
+        this.department = department;
+    }
+
+    public String getPost() {
+        return post;
+    }
+
+    public void setPost(String post) {
+        this.post = post;
+    }
+
+    public String getRole() {
+        return role;
+    }
+
+    public void setRole(String role) {
+        this.role = role;
+    }
+
+    public Date getCreateDate() {
+        return createDate;
+    }
+
+    public void setCreateDate(Date createDate) {
+        this.createDate = createDate;
+    }
+
+    public Date getUpdateDate() {
+        return updateDate;
+    }
+
+    public void setUpdateDate(Date updateDate) {
+        this.updateDate = updateDate;
+    }
+}

+ 2 - 1
src/main/java/com/model/pojo/RepCode.java

@@ -11,7 +11,8 @@ public enum  RepCode {
     hasGroupUsing(-140, "存在子分组"),
     hasConUsing(-150, "分组正在被数据源使用"),
     hasChartUsing(-160, "分组正在被图表使用"),
-    ChartsNameNull(-170, "表不存在");
+    ChartsNameNull(-170, "表不存在"),
+    NoUser(-180, "用户名或密码错误");
 
     private int code;
     private String msg;

+ 22 - 0
src/main/java/com/model/vo/configVo/LoginInfo.java

@@ -0,0 +1,22 @@
+package com.model.vo.configVo;
+
+public class LoginInfo {
+    private String userName;
+    private String passWord;
+
+    public String getUserName() {
+        return userName;
+    }
+
+    public void setUserName(String userName) {
+        this.userName = userName;
+    }
+
+    public String getPassWord() {
+        return passWord;
+    }
+
+    public void setPassWord(String passWord) {
+        this.passWord = passWord;
+    }
+}

+ 67 - 0
src/main/java/com/model/vo/configVo/UserInfo.java

@@ -0,0 +1,67 @@
+package com.model.vo.configVo;
+
+public class UserInfo {
+    private int id;
+    private String name;
+    private String userName;
+    private String passWord;
+    private String department;
+    private String post;
+    private String role;
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getUserName() {
+        return userName;
+    }
+
+    public void setUserName(String userName) {
+        this.userName = userName;
+    }
+
+    public String getPassWord() {
+        return passWord;
+    }
+
+    public void setPassWord(String passWord) {
+        this.passWord = passWord;
+    }
+
+    public String getDepartment() {
+        return department;
+    }
+
+    public void setDepartment(String department) {
+        this.department = department;
+    }
+
+    public String getPost() {
+        return post;
+    }
+
+    public void setPost(String post) {
+        this.post = post;
+    }
+
+    public String getRole() {
+        return role;
+    }
+
+    public void setRole(String role) {
+        this.role = role;
+    }
+}

+ 2 - 0
src/main/java/com/server/ColumnScreenService.java

@@ -27,6 +27,8 @@ public class ColumnScreenService {
         String tableName = chartsConfigMapper.getTableName(id);
         if (tableName == null || "".equals(tableName)){
             return new RepEntity(RepCode.nullTableName);
+        }else {
+            tableName = "(" + tableName +")";
         }
 
         List<Object> data = new ArrayList<>();

+ 1 - 1
src/main/java/com/server/DataBasesService.java

@@ -158,7 +158,7 @@ public class DataBasesService {
         if (target.size() == 0) {
             target.putAll(dataSourceRegister.getSlaveDataSources());
         }
-        target.put(databasesInfo.getUserName(), dataSourceRegister.buildDataSource(dsMap));
+        target.put(databasesInfo.getId(), dataSourceRegister.buildDataSource(dsMap));
         DynamicDataSource datasource = (DynamicDataSource) ContextUtil.getBean("dataSource");
         datasource.setTargetDataSources(target);
         datasource.afterPropertiesSet();

+ 0 - 3
src/main/java/com/server/ShowHistogramService.java

@@ -15,7 +15,6 @@ import com.util.CalculationJudgeUtil;
 import com.util.ScreenUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Repository;
-import org.springframework.transaction.annotation.Transactional;
 
 import java.util.ArrayList;
 import java.util.Iterator;
@@ -41,7 +40,6 @@ public class ShowHistogramService {
     /*
     柱状图数据展示
      */
-    @Transactional
     public RepEntity showHistogram(HistogramConfigInfo histogramConfigInfo){
         ChartsDataInfo chartsDataInfo = new ChartsDataInfo();
         if (histogramConfigInfo == null || "".equals(histogramConfigInfo)){
@@ -58,7 +56,6 @@ public class ShowHistogramService {
         }
 
         int baseId = getChartsDataUtilService.getBaseId(id);
-//        int baseId =
         System.out.println("切换数据库"+baseId);
 
         try{

+ 37 - 0
src/main/java/com/server/UserService.java

@@ -0,0 +1,37 @@
+package com.server;
+
+import com.dao.UserMapper;
+import com.model.po.TokenData;
+import com.model.po.User;
+import com.model.pojo.RepCode;
+import com.model.pojo.RepEntity;
+import com.model.vo.configVo.LoginInfo;
+import com.util.JwtTokenUtil;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service
+public class UserService {
+    @Autowired
+    JwtTokenUtil jwtTokenUtil;
+    @Autowired
+    UserMapper userMapper;
+
+    public RepEntity login(LoginInfo loginInfo){
+        String userName = loginInfo.getUserName();
+        User user = userMapper.getLogin(userName);
+        System.out.println("user:" + user);
+        if ("".equals(user) || user == null){
+            return new RepEntity(RepCode.NoUser);
+        }
+        String passWord = user.getPassWord();
+        if (!passWord.equals(loginInfo.getPassWord())){
+            System.out.println("....passw:" + passWord + "ps:" + loginInfo.getPassWord());
+            return new RepEntity(RepCode.NoUser);
+        }
+
+        TokenData tokenData = jwtTokenUtil.createToke(user);
+        userMapper.updateToken(tokenData.getToken(),user.getId());
+        return new RepEntity(RepCode.success, tokenData);
+    }
+}

+ 1 - 1
src/main/java/com/util/BasesSource/DynamicDataSourceRegister.java

@@ -112,7 +112,7 @@ public class DynamicDataSourceRegister implements ImportBeanDefinitionRegistrar,
             rs = pstmt.executeQuery();//运行查询操作
             System.out.println();
             while(rs.next()){
-                System.out.println("userName:" +rs.getString("user_name"));
+                System.out.println("id:" +rs.getString("id"));
                 // 多个数据源
                 Map<String, Object> dsMap = new HashMap<>();
                 String dei = rs.getString("database_type");

+ 96 - 0
src/main/java/com/util/JwtTokenUtil.java

@@ -0,0 +1,96 @@
+package com.util;
+
+import com.auth0.jwt.JWT;
+import com.auth0.jwt.JWTVerifier;
+import com.auth0.jwt.algorithms.Algorithm;
+import com.auth0.jwt.interfaces.Claim;
+import com.auth0.jwt.interfaces.DecodedJWT;
+import com.model.po.TokenData;
+import com.model.po.User;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Before;
+import org.aspectj.lang.annotation.Pointcut;
+import org.springframework.core.annotation.Order;
+import org.springframework.stereotype.Component;
+
+import java.io.UnsupportedEncodingException;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+@Aspect
+@Component
+public class JwtTokenUtil {
+    //公共密钥
+    public static String SECRET = "YouRuanKeJiBi";
+
+    @Pointcut("@annotation(com.controller.CheckToken)")
+    public void CheckToken() {
+
+    }
+
+    public TokenData createToke(User user){
+
+        //签发时间
+        Date iaDate = new Date();
+
+        //过期时间
+        Calendar nowTime = Calendar.getInstance();
+        nowTime.add(Calendar.MINUTE, 30);
+        Date expiresDate = nowTime.getTime();
+
+        //转换成时间戳
+        String res;
+        long ts = expiresDate.getTime();
+        res = String.valueOf(ts);
+        System.out.println("time:"+ ts);
+
+        //设置header信息
+        Map<String, Object> map = new HashMap<String, Object>();
+        map.put("alg", "HS256");
+        map.put("typ", "JWT");
+        String token = "";
+
+        try {
+            token = JWT.create()
+                    .withHeader(map)
+                    .withClaim("id", user.getId())
+                    .withClaim("name", user.getName())
+                    .withClaim("department", user.getDepartment())
+                    .withClaim("post", user.getPost())
+                    .withClaim("role", user.getRole())
+                    .withExpiresAt(expiresDate)
+                    .withIssuedAt(iaDate)
+                    .sign(Algorithm.HMAC256(SECRET));
+            System.out.println("token:::::::::::" + token);
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+        }
+        TokenData tokenData = new TokenData();
+        tokenData.setToken(token);
+        tokenData.setTimes(res);
+        return tokenData ;
+    }
+
+    //解密
+    @Before("CheckToken()")
+    @Order(1)
+    public static Map<String, Claim> verifyToken(JoinPoint joinPoint) throws Exception {
+        DecodedJWT jwt = null;
+
+        Object[] arg = joinPoint.getArgs();
+        String token = (String) arg[0];
+        System.out.println("token:::::" + token);
+
+        JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET)).build();
+
+        try {
+            jwt = verifier.verify(token);
+        } catch (Exception e) {
+            throw new RuntimeException("登录凭证已过期,请重新登录");
+        }
+        return jwt.getClaims();
+    }
+}