Commit ca103731c721aae3f5f622e58c679ed5c09fd9e9

Authored by Kairlec
1 parent 512b0cc8

fix response `content-type` is not set if access without access token

src/main/java/com/genersoft/iot/vmp/conf/security/AnonymousAuthenticationEntryPoint.java
@@ -3,6 +3,7 @@ package com.genersoft.iot.vmp.conf.security; @@ -3,6 +3,7 @@ package com.genersoft.iot.vmp.conf.security;
3 import com.alibaba.fastjson2.JSONObject; 3 import com.alibaba.fastjson2.JSONObject;
4 import com.genersoft.iot.vmp.conf.security.dto.JwtUser; 4 import com.genersoft.iot.vmp.conf.security.dto.JwtUser;
5 import com.genersoft.iot.vmp.vmanager.bean.ErrorCode; 5 import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
  6 +import org.springframework.http.MediaType;
6 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 7 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
7 import org.springframework.security.core.AuthenticationException; 8 import org.springframework.security.core.AuthenticationException;
8 import org.springframework.security.core.context.SecurityContextHolder; 9 import org.springframework.security.core.context.SecurityContextHolder;
@@ -12,6 +13,7 @@ import org.springframework.stereotype.Component; @@ -12,6 +13,7 @@ import org.springframework.stereotype.Component;
12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse; 14 import javax.servlet.http.HttpServletResponse;
14 import java.io.IOException; 15 import java.io.IOException;
  16 +import java.nio.charset.StandardCharsets;
15 17
16 /** 18 /**
17 * 处理匿名用户访问逻辑 19 * 处理匿名用户访问逻辑
@@ -35,6 +37,8 @@ public class AnonymousAuthenticationEntryPoint implements AuthenticationEntry @@ -35,6 +37,8 @@ public class AnonymousAuthenticationEntryPoint implements AuthenticationEntry
35 jsonObject.put("msg", e.getMessage()); 37 jsonObject.put("msg", e.getMessage());
36 } 38 }
37 response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); 39 response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
  40 + response.setContentType(MediaType.APPLICATION_JSON_VALUE);
  41 + response.setCharacterEncoding(StandardCharsets.UTF_8.name());
38 try { 42 try {
39 response.getWriter().print(jsonObject.toJSONString()); 43 response.getWriter().print(jsonObject.toJSONString());
40 } catch (IOException ioException) { 44 } catch (IOException ioException) {