SingelSignOn之Security配置类

注:本业务的认证业务只做认证,故而最后将所有请求放行,本帖供不同业务参考

  protected void configure(HttpSecurity http) throws Exception {
        //super.configure(http); 默认匿名登录(所有资源都放行)
        http.csrf().disable();//禁用跨域攻击 否则无法使用postman进行测试
        http.formLogin().successHandler(successHandler())
                        .failureHandler(failureHandler()) ;
    }

1.使用SpringSecurity安全框架定义Security配置类(配置认证规则)

(1)加密并重写configure方法:

 此方法为http请求配置方法,可以在此方法中配置:
 1)哪些资源放行(不用登录即可访问),假如不做任何配置默认所有资源都匿名访问
 2)哪些资源必须认证(登录)后才可访问。
  @Bean  //密码加密对象
    public PasswordEncoder passwordEncoder(){
        //底层调用boolean matches(CharSequence var1, String var2);
        //对密码进行加密 随机盐值 比MD5更安全
        return new BCryptPasswordEncoder();
    }

    protected void configure(HttpSecurity http) throws Exception {}
     /**
     * 此方法为http请求配置方法,可以在此方法中配置:
     * 1)哪些资源放行(不用登录即可访问),假如不做任何配置默认所有资源都匿名访问
     * 2)哪些资源必须认证(登录)后才可访问。
     */
http.csrf().disable();//禁用跨域攻击 否则无法使用postman等第三方工具进行测试
protected void configure(HttpSecurity http) throws Exception {
        //super.configure(http); 默认匿名登录(所有资源都放行)
        http.csrf().disable();//禁用跨域攻击 否则无法使用postman进行测试
    }
http.authorizeRequests().anyRequest().permitAll();//authorize认证 全部放行
protected void configure(HttpSecurity http) throws Exception {
        //super.configure(http); 默认匿名登录(所有资源都放行)
        http.csrf().disable();//禁用跨域攻击 否则无法使用postman进行测试
        http.authorizeRequests().anyRequest().permitAll();//authorize认证 全部放行
    }
http.authorizeRequests().antMatchers("/**").authenticated();//全部拦截(需要认证才放行)
protected void configure(HttpSecurity http) throws Exception {
      //super.configure(http); 默认匿名登录(所有资源都放行)
      http.csrf().disable();//禁用跨域攻击 否则无法使用postman进行测试
      http.authorizeRequests().antMatchers("/**").authenticated();//全部拦截(需要认证才放行)
    }
http.authorizeRequests().antMatchers("/index.html").authenticated()
        .anyRequest().permitAll();//只拦截/index.html 其他全放行
  protected void configure(HttpSecurity http) throws Exception {
        //super.configure(http); 默认匿名登录(所有资源都放行)
        http.csrf().disable();//禁用跨域攻击 否则无法使用postman进行测试
        http.authorizeRequests().antMatchers("/index.html").authenticated()
                .anyRequest().permitAll();//只拦截/index.html 其他全放行
  
    }

2.定义认证成功和失败后反馈的页面

http.formLogin().successForwardUrl("/success.html")
http.formLogin()..failureForwardUrl("/fail.html");
   protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.authorizeRequests().antMatchers("/index.html").authenticated()
                .anyRequest().permitAll();//只拦截/index.html 其他全放行
        http.formLogin().successForwardUrl("/success.html")
                        .failureForwardUrl("/fail.html");
}

但是一般返回前端json串,前后端分离中的一种做法,是登录成功要返回json数据

所以 还可自定义登陆成功处理器

http.formLogin().successHandler(successHandler())
                .failureHandler(failureHandler());
  @Override
    protected void configure(HttpSecurity http) throws Exception {
        //super.configure(http); 默认匿名登录(所有资源都放行)
        http.csrf().disable();//禁用跨域攻击 否则无法使用postman进行测试
        http.authorizeRequests().anyRequest().permitAll();//authorize认证 全部放行
        http.formLogin().successHandler(successHandler())
                        .failureHandler(failureHandler());
    }
private AuthenticationFailureHandler failureHandler() {
        return (httpServletRequest, httpServletResponse, authentication) -> {
            Map<String,Object> map=new HashMap();
            map.put("status",500);
            map.put("message", "login error");
            writeJsonToClient(httpServletResponse, map);
        };
    }


    private AuthenticationSuccessHandler successHandler() {
        return (httpServletRequest, httpServletResponse, authentication) -> {
            Map<String,Object> map=new HashMap();
            map.put("status",200);
            map.put("message", "ok");
            writeJsonToClient(httpServletResponse, map);
        };
    }

抽出共性

 private void writeJsonToClient(HttpServletResponse response,Map map) throws IOException {
        String json = new ObjectMapper().writeValueAsString(map);
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.println(json);
        writer.flush();
    }

也可使用:(重定向)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值