Переглянути джерело

更新了配置文件信息

wuweihao 4 роки тому
батько
коміт
df872443c4

+ 58 - 0
README.md

@@ -25,9 +25,14 @@ http://39.108.123.31/data/wuyidaxue/14/13336/7134.png
 build
 http:loacalhost:8082/data/aa.jpg
 
+
+# sit
+
 数据管理平台:
 http://47.107.252.54:8082/back
 
+
+
 47.107.252.54 
     tomcat:/data/tomcat_cesium_8082
 
@@ -56,4 +61,57 @@ http://map.4dage.com/3dmap/
     数据管理平台访问url: http://19.121.251.13:8080/back
     后台api: http://19.121.251.13:8080/test/test
     后台api: http://19.121.251.13:8080/doc.html
+ 
+ 
+# 需求
+  管理员:看所有数据
+  部门经理:看所有部门的数据
+  员工:看自己的数据
+ 
+ 
+##################### 2020-11-11 #####################
+    1. 更换了新服务器, ip:47.112.166.173
+    2. tomcat移动到 root/user/java
+    
+    
+ 
+
+
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
  

+ 34 - 0
src/main/java/com/fd/exception/JwtAuthenticationException.java

@@ -0,0 +1,34 @@
+package com.fd.exception;
+
+import org.apache.shiro.ShiroException;
+
+public class JwtAuthenticationException extends ShiroException {
+
+    private static final long serialVersionUID = 2899335020273674736L;
+
+    private int code;
+
+    private String msg;
+
+    public JwtAuthenticationException(int code, String msg){
+        super(msg);
+        this.code = code;
+        this.msg = msg;
+    }
+
+    public int getCode() {
+        return code;
+    }
+
+    public void setCode(int code) {
+        this.code = code;
+    }
+
+    public String getMsg() {
+        return msg;
+    }
+
+    public void setMsg(String msg) {
+        this.msg = msg;
+    }
+}

+ 50 - 41
src/main/java/com/fd/shiro/JWTFilter.java

@@ -1,5 +1,7 @@
 package com.fd.shiro;
 
+import com.alibaba.fastjson.JSONObject;
+import com.fd.exception.JwtAuthenticationException;
 import lombok.extern.log4j.Log4j2;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.shiro.authc.AuthenticationException;
@@ -7,11 +9,13 @@ import org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter;
 import org.springframework.http.HttpStatus;
 import org.springframework.web.bind.annotation.RequestMethod;
 
+import javax.servlet.ServletOutputStream;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
+import java.io.PrintWriter;
 
 
 /**
@@ -23,38 +27,19 @@ public class JWTFilter extends BasicHttpAuthenticationFilter {
 
 
     /**
-     * 判断用户是否想要登入。
-     * 检测header里面是否包含Authorization字段即可
-     */
-    @Override
-    protected boolean isLoginAttempt(ServletRequest request, ServletResponse response) {
-        HttpServletRequest req = (HttpServletRequest) request;
-        String authorization = req.getHeader("Authorization");
-
-        if (StringUtils.isEmpty(authorization)) {
-            log.info("error Authorization is null");
-
-            // 先这样抛出异常,这个种不是接口的形式
-            throw new AuthenticationException("Authorization is null ");
-        }
-
-        return true;
-
-    }
-
-
-
-    /**
      * 执行登录验证
      */
     @Override
     protected boolean executeLogin(ServletRequest request, ServletResponse response) {
-//        LOGGER.warn("run executeLogin");
         HttpServletRequest httpServletRequest = (HttpServletRequest) request;
         String authorization = httpServletRequest.getHeader("Authorization");
-        JWTToken token = new JWTToken(authorization);
 
-        // 判断token 是否跟redis
+        if (authorization == null) {
+            throw new JwtAuthenticationException(5005, "Authorization is null");
+        }
+
+
+        JWTToken token = new JWTToken(authorization);
 
         // 提交给realm进行登入,如果错误他会抛出异常并被捕获
         getSubject(request, response).login(token);
@@ -81,21 +66,35 @@ public class JWTFilter extends BasicHttpAuthenticationFilter {
      */
     @Override
     protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
-//        LOGGER.warn("run isAccessAllowed");
-        if (isLoginAttempt(request, response)) {
-//            try {
-//                executeLogin(request, response);
-//            } catch (Exception e) {
-////                response401(request, response);
-//                throw new AuthenticationException("Authorization is null 123");
-//            }
-
+        try {
             executeLogin(request, response);
+            return true;
+        } catch (Exception e) {
+            /** 这个异常需要自己写,全局捕获不了*/
+
+            // 认证出现异常,传递错误信息msg
+            String msg = e.getMessage();
+            // 获取应用异常(该Cause是导致抛出此throwable(异常)的throwable(异常))
+            Throwable throwable = e.getCause();
+            JSONObject jsonObject = new JSONObject();
+            if (throwable instanceof JwtAuthenticationException) {
+                jsonObject.put("status", ((JwtAuthenticationException) throwable).getCode());
+                jsonObject.put("message", ((JwtAuthenticationException) throwable).getMsg());
+            }else{
+                log.error(msg);
+                jsonObject.put("status", 5002);
+                jsonObject.put("message", "token invalid");
+            }
+            // 直接返回Response信息
+            this.writeResponse(response, jsonObject);
+
+            return false;
         }
-        // return false 前端没有响应,接收不到异常
-        return true;
     }
 
+
+
+
     /**
      * 对跨域提供支持
      * 只对需要token验证的有效,不需要验证的还是需要用注解处理一下
@@ -116,15 +115,25 @@ public class JWTFilter extends BasicHttpAuthenticationFilter {
         return super.preHandle(request, response);
     }
 
+
     /**
-     * 将非法请求跳转到 /401
+     * 无需转发,直接返回Response信息
+     *
      */
-    private void response401(ServletRequest req, ServletResponse resp) {
+    private void writeResponse(ServletResponse response, JSONObject msg) {
+        response.setCharacterEncoding("UTF-8");
+        response.setContentType("application/json; charset=utf-8");
+        ServletOutputStream outputStream = null;
         try {
-            HttpServletResponse httpServletResponse = (HttpServletResponse) resp;
-            httpServletResponse.sendRedirect("/401");
+            outputStream = response.getOutputStream();
         } catch (IOException e) {
-            log.error(e.getMessage());
+            e.printStackTrace();
         }
+        assert outputStream != null;
+        PrintWriter printWriter = new PrintWriter(outputStream, true);
+        printWriter.write(msg.toString());//直接将json输出到页面
+        printWriter.flush();
+        printWriter.close();
+
     }
 }

+ 5 - 8
src/main/java/com/fd/shiro/MyRealm.java

@@ -1,6 +1,7 @@
 package com.fd.shiro;
 
 import com.fd.entity.UserEntity;
+import com.fd.exception.JwtAuthenticationException;
 import com.fd.repository.UserRepository;
 import lombok.extern.log4j.Log4j2;
 import org.apache.shiro.authc.AuthenticationException;
@@ -67,38 +68,34 @@ public class MyRealm extends AuthorizingRealm {
      */
     @Override
     protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken auth) throws AuthenticationException {
-//        log.warn("run doGetAuthenticationInfo");
         String token = (String) auth.getCredentials();
-//        log.warn("token: {}", token);
         // 解密获得username,用于和数据库进行对比
         String username = JWTUtil.getUsername(token);
         if (username == null) {
             log.info("error token username");
-            throw new AuthenticationException("token invalid");
+            throw new JwtAuthenticationException(5001, "token invalid");
         }
 
         UserEntity user = userRepository.findByUsername(username);
         if (user == null) {
             log.info("error token user");
-            throw new AuthenticationException("User didn't existed!");
+            throw new JwtAuthenticationException(5001, "token invalid");
         }
 
         // 校验请求token是否跟redis token一致
         String redisToken = (String) redisTemplate.opsForValue().get(user.getUsername());
         if (!token.equals(redisToken)) {
             log.info("error token redis");
-//            throw new AuthenticationException("token invalid");
-            throw new AuthenticationException("token invalid");
+            throw new JwtAuthenticationException(5001, "token invalid");
 
         }
 
 
         if (! JWTUtil.verify(token, username, user.getPassword())) {
             log.info("error token username or password");
-            throw new AuthenticationException("token invalid");
+            throw new JwtAuthenticationException(5001, "token invalid");
         }
 
-//        log.warn("end doGetAuthenticationInfo");
 
         return new SimpleAuthenticationInfo(token, token, "my_realm");
     }

+ 3 - 9
src/main/resources/application-sit.properties

@@ -1,13 +1,8 @@
 
-#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
-#spring.datasource.url=jdbc:mysql://47.107.252.54:3306/gis_cesium?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
-#spring.datasource.username=root
-#spring.datasource.password=4dkankan4dage
-
 # \u6570\u636E\u6E90\u914D\u7F6E
 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
-spring.datasource.druid.url=jdbc:mysql://47.107.252.54:3306/gis_cesium?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
+spring.datasource.druid.url=jdbc:mysql://127.0.0.1:3306/gis_cesium?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
 spring.datasource.druid.username=root
 spring.datasource.druid.password=4dkankan4dage
 
@@ -36,8 +31,7 @@ spring.datasource.druid.stat-view-servlet.enabled=true
 
 # Redis\u6570\u636E\u5E93\u7D22\u5F15\uFF08\u9ED8\u8BA4\u4E3A0\uFF09
 spring.redis.database=0
-#spring.redis.host=127.0.0.1
-spring.redis.host=47.107.252.54
+spring.redis.host=127.0.0.1
 spring.redis.port=6379
 spring.redis.password=4dage
 # \u8FDE\u63A5\u8D85\u65F6\u65F6\u95F4 \u5355\u4F4D ms\uFF08\u6BEB\u79D2\uFF09
@@ -51,7 +45,7 @@ spring.redis.jedis.pool.max-active=8
 # \u7B49\u5F85\u53EF\u7528\u8FDE\u63A5\u7684\u6700\u5927\u65F6\u95F4\uFF0C\u5355\u4F4D\u6BEB\u79D2\uFF0C\u9ED8\u8BA4\u503C\u4E3A-1\uFF0C\u8868\u793A\u6C38\u4E0D\u8D85\u65F6\u3002\u5982\u679C\u8D85\u8FC7\u7B49\u5F85\u65F6\u95F4\uFF0C\u5219\u76F4\u63A5\u629B\u51FAJedisConnectionException
 spring.redis.jedis.pool.max-wait=-1ms
 
-logging.file=/data/tomcat_cesium_8082/log/cesium.log
+logging.file=/root/user/java/tomcat_cesium_8082/log/cesium.log
 
 base.path=/root/gis/data