• 13-security其他.md


    跨域处理

    //允许跨域
    http.cors();
    
    • 1
    • 2

    放行swagger相关页面

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
                .antMatchers(AUTH_WHITELIST); // 忽略swagger ui静态资源
    }
    
    // -- swagger ui忽略
    private static final String[] AUTH_WHITELIST = {
            // -- swagger ui
            "/swagger-resources/**",
            "/swagger-ui.html",
            "/v2/api-docs",
            "/webjars/**",
            // swagger-boostrap-ui
            "/doc.html"
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    异常处理

    ​ 我们还希望在认证失败或者是授权失败的情况下也能和我们的接口一样返回相同结构的json,这样可以让前端能对响应进行统一的处理。要实现这个功能我们需要知道SpringSecurity的异常处理机制。

    ​ 在SpringSecurity中,如果我们在认证或者授权的过程中出现了异常会被ExceptionTranslationFilter捕获到。在ExceptionTranslationFilter中会去判断是认证失败还是授权失败出现的异常。

    ​ 如果是认证过程中出现的异常会被封装成AuthenticationException然后调用AuthenticationEntryPoint对象的方法去进行异常处理。

    ​ 如果是授权过程中出现的异常会被封装成AccessDeniedException然后调用AccessDeniedHandler对象的方法去进行异常处理。

    ​ 所以如果我们需要自定义异常处理,我们只需要自定义AuthenticationEntryPoint和AccessDeniedHandler然后配置给SpringSecurity即可。

    ​ 先注入对应的处理器

        @Autowired
        private AuthenticationEntryPoint authenticationEntryPoint;
    
        @Autowired
        private AccessDeniedHandler accessDeniedHandler;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    ​ 然后我们可以使用HttpSecurity对象的方法去配置。

            http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint).
                    accessDeniedHandler(accessDeniedHandler);
    
    • 1
    • 2

    实现类

    package com.zs.admin.security.exception;
    
    import com.alibaba.fastjson.JSON;
    import com.zs.model.common.dtos.ResponseResult;
    import com.zs.utils.web.WebUtils;
    import org.springframework.http.HttpStatus;
    import org.springframework.security.access.AccessDeniedException;
    import org.springframework.security.web.access.AccessDeniedHandler;
    import org.springframework.stereotype.Component;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @Component
    public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
        @Override
        public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
            ResponseResult result = new ResponseResult(HttpStatus.FORBIDDEN.value(), "权限不足");
            String json = JSON.toJSONString(result);
            WebUtils.renderString(response,json);
        }
    }
    
    
    
    /**
     * 用户权限处理
     * @author 11921
     */
    @Component
    public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    
        @Override
        public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
            httpServletResponse.setContentType("application/json;charset=utf-8");
            httpServletResponse.getWriter().write(JSON.toJSONString(new Result(false, StatusConst.AUTHORIZED,"没有操作权限")));
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    package com.zs.admin.security.exception;
    
    import com.alibaba.fastjson.JSON;
    import com.zs.model.common.dtos.ResponseResult;
    import com.zs.utils.web.WebUtils;
    import org.springframework.http.HttpStatus;
    import org.springframework.security.core.AuthenticationException;
    import org.springframework.security.web.AuthenticationEntryPoint;
    import org.springframework.stereotype.Component;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    
    @Component
    public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    
        @Override
        public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
            ResponseResult result = new ResponseResult(HttpStatus.UNAUTHORIZED.value(), "认证失败请重新登录");
            String json = JSON.toJSONString(result);
            WebUtils.renderString(response,json);
        }
    }
    
    
    
    /**
     * 用户未登录处理
     *
     * @author 11921
     */
    @Component
    public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    
        @Override
        public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException {
            httpServletResponse.setContentType("application/json;charset=utf-8");
            httpServletResponse.getWriter().write(JSON.toJSONString(new Result(false, StatusConst.NOT_LOGIN, "请登录")));
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
  • 相关阅读:
    vue pdf文件流 预览
    python pyqt5 计算下载文件的进度百分比
    Python环境搭建(Python、 Pycharm安装/pycharm创建python虚拟环境Virtualenv)基础
    ChromeDriver全版本下载教程
    c++开发面试题
    Canal1--搭建Canal监听数据库变化
    移动应用安全
    【C#】某AGV调度系统源码笔记(十二)
    5分钟立马学会使用python的pymysql库操作Mysql
    MFC 工具栏响应事件实例
  • 原文地址:https://blog.csdn.net/weixin_44235759/article/details/126106395