基于角色得后台权限管理系统设计(六、spring security 让异常返回json数据而不是页面跳转)

第一次故意输错密码

第二次使用admin登入,然后反问user页面的getUser

 

统一定义返回

/**
 * @Auth yaozhongjie
 * @Date 2019/7/3 20:44
 **/
public class Render {
    public static void respJson(String msg,HttpServletResponse httpServletResponse){
        httpServletResponse.setContentType("application/json");
        httpServletResponse.setCharacterEncoding("utf-8");
        PrintWriter writer = null;
        try {
            writer = httpServletResponse.getWriter();
            writer.write(JSON.toJSONString(Result.error(msg),SerializerFeature.WriteMapNullValue));
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            writer.close();
        }
    }
}

Result类标准化输出。需要lombok插件

/**
 * @Auth yaozhongjie
 * @Date 2019/6/28 9:56
 **/
@Data
public class Result<T> {
    private Integer code;
    private String msg;
    private T data;


    public static Result success(String msg){
        Result result=new Result();
        result.code=0;
        result.msg=msg;
        return result;
    }

    public static Result success(JSONObject data){
        Result result=new Result();
        result.code=0;
        result.msg="success";
        result.data=data;
        return result;
    }

    public static Result error(String msg){
        Result result=new Result();
        result.code=-1;
        result.msg=msg;
        return result;
    }

}

 

添加权限异常处理

/**
 * @Auth yaozhongjie
 * @Date 2019/7/3 20:37
 **/
@Component
public class MyAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        Render.respJson("无权访问",httpServletResponse);
    }
}

添加授权失败处理

/**
 * @Auth yaozhongjie
 * @Date 2019/7/3 20:43
 **/
@Component
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        Render.respJson("认证失败",httpServletResponse);
    }
}

httpSecurity中配置异常处理器

    /*
    @Autowired
    MyAccessDeniedHandler accessDeniedHandler;
    @Autowired
    MyAuthenticationFailureHandler authenticationFailureHandler;

    */

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .mvcMatchers("/data/*").hasRole("ADMIN")
                .mvcMatchers("/admin/*").hasRole("ADMIN")
                .mvcMatchers("/user/*").hasRole("USER")
                .anyRequest()
                .authenticated()
        ;
        //注意认证失败处理在这里配置
        http.formLogin().failureHandler(authenticationFailureHandler).permitAll();
        //权限校验失败处理在这配置
        http.exceptionHandling()
                .accessDeniedHandler(accessDeniedHandler);
    }

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1:集成最新,最流行,,最成熟悉的技术框架 springMVC4.1.4,spring4.1.4,mybaits3.2.8,MySQL5.6 2:前端界面采用当前最流行的响应式布局设计 bootstrap3,html5,CSS3,JQueryAjax 特点: 1:基于mybaits插件完成对于基本的增,删,改,查的功能,不需要写任何SQL语句,代码一键调用即可.具有很好的灵活性,方便性,快速性.这样就大大减少代码开发时间,提高开发效率,为企业或个人有效率地节约人力资源的成本! 2:对JAVA的封装,多态,继承特性做了充分的运用,其中也用到了JAVA的一个重要机制--反射机制! 3:对于简单业务的单表增,删,改,查只需要一个entity,不需要写任何的controller,service,mapper等代码,也不需要任何的sql语句,让程序员更专注于前端 4:具有安全,可靠能快速,方便,高效地进行开发 5:系统具有可扩展性,可控性,通用性强的基础 6:基础的业务性不强的列表界面采取直接配置方式,不需要写任何代码,直接生成界面,具有快捷,快速,方便地查看实时功能效果 7:集成监控功能,随时监控系统内存,CPU,磁盘的最新变化 8:新增告警邮件触发功能,让用户随时了解服务器状况 目标: 1:提升开发效率,规范开发标准 2:提高质量,降低复杂度 3:打造一个开发简单.效率快..性能好.. 易上手的框架 兼容性: 1:设备:兼容IPAD,手机,电脑,笔记本等等 2:界面:兼容IE8+,firefox,google等等 3:数据库:兼容MySQL,oracel,sqlserver2008 通用性: 让项目只注重业务,为企业或个人有效率地节约人力资源的成本! 1:系统具有最基本的用户,角色,资源,权限管理,日志管理等通用性功能,企业或个人可直接在此基础上进行开发,扩展,添加各自的需求和业务功能!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值