java110
2023-05-09 669da79f9e99ee0025f5280285591b10a4f0f002
springboot/src/main/java/com/java110/boot/aop/PageProcessAspect.java
@@ -192,18 +192,17 @@
     */
    private String getToken(HttpServletRequest request) throws FilterException {
        String token = "";
        if (request.getCookies() == null || request.getCookies().length == 0) {
            return token;
        }
        for (Cookie cookie : request.getCookies()) {
            if (CommonConstant.COOKIE_AUTH_TOKEN.equals(cookie.getName())) {
                token = cookie.getValue();
        if (request.getCookies() != null && request.getCookies().length > 0) {
            for (Cookie cookie : request.getCookies()) {
                if (CommonConstant.COOKIE_AUTH_TOKEN.equals(cookie.getName())) {
                    token = cookie.getValue();
                }
            }
        }
        String authorization = request.getHeader("Authorization");
        if(StringUtil.isEmpty(token) && !StringUtil.isEmpty(authorization)){
            token = token.substring("Bearer ".length());
        if (StringUtil.isEmpty(token) && !StringUtil.isEmpty(authorization)) {
            token = authorization.substring("Bearer ".length());
        }
        return token;
    }
@@ -217,13 +216,16 @@
     * @throws IOException
     */
    private void writeCookieInfo(IPageData pd, ServletRequestAttributes attributes) throws IOException {
        // 这里目前只写到组件级别,如果需要 写成方法级别
        if (!StringUtil.isNullOrNone(pd.getToken()) && "login".equals(pd.getComponentCode())) {
        // 这里目前只写到组件级别,如果需要 写成方法级别 && "login".equals(pd.getComponentCode())
        if (!StringUtil.isNullOrNone(pd.getToken()) ) {
            HttpServletResponse response = attributes.getResponse();
            Cookie cookie = new Cookie(CommonConstant.COOKIE_AUTH_TOKEN, pd.getToken());
            cookie.setHttpOnly(true);
            cookie.setPath("/");
            response.addCookie(cookie);
            //response.addHeader("Set-Cookie","SameSite=None");
            response.flushBuffer();
        }