springSecurity login 404 ,rbac不走

login  404 

是因为在AUTH_WHITELIST放行的url加了/login

 

rbac不走是因为在AUTH_WHITELIST放行的url加了/error

 

AUTH_WHITELIST  是忽略走rbac,但是jwtfilter里面不能忽略,login还是会走fiter,不知道为啥

 

 

 

package com.bmsoft.behavioranalysis.server.tenant.common.config;


import com.bmsoft.behavioranalysis.server.tenant.security.hander.AjaxAccessDeniedHandler;
import com.bmsoft.behavioranalysis.server.tenant.security.hander.AjaxAuthenticationEntryPoint;
import com.bmsoft.behavioranalysis.server.tenant.security.hander.AjaxAuthenticationFailureHandler;
import com.bmsoft.behavioranalysis.server.tenant.security.hander.AjaxAuthenticationSuccessHandler;
import com.bmsoft.behavioranalysis.server.tenant.security.hander.AjaxLogoutSuccessHandler;
import com.bmsoft.behavioranalysis.server.tenant.security.login.CustomAuthenticationProvider;
import com.bmsoft.behavioranalysis.server.tenant.security.permission.JwtAuthenticationTokenFilter;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationDetailsSource;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.WebAuthenticationDetails;


@Configuration  //配置类
@EnableWebSecurity  //开启权限
@EnableGlobalMethodSecurity(prePostEnabled = true)  //开启权限注解
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


  /**
   * 需要放行的URL
   */
  private static final String[] AUTH_WHITELIST = {
      "/druid/**",
      "/v2/api-docs/**",
      "/swagger-resources/**",
      "/configuration/ui/**",
      "/configuration/security/**",
      "/swagger-ui.html/swagger-resources",
      "/swagger-ui.html",
      "/webjars/**",
      "/index.html",
      "/static/**",
      "/api/**",
      "/login_p",
      "/serverTenant/login",
      "/serverTenant/sysTenant/getTenantName",
      "/serverTenant/sysTenantDetail/getPlateSuccessCode",
      "/serverTenant/sysTenantDetail/getUrl",
      "/menu/menu",
      "/doLogin",
      "/",
      "/csrf"
  };


  @Autowired
  private AjaxAuthenticationEntryPoint authenticationEntryPoint;  //未登陆时返回 JSON 格式的数据给前端(否则为 html)
  @Autowired
  private AjaxAuthenticationSuccessHandler authenticationSuccessHandler;   //登录成功返回的 JSON 格式数据给前端(否则为 html)
  @Autowired
  private AjaxAuthenticationFailureHandler authenticationFailureHandler;   //登录失败返回的 JSON 格式数据给前端(否则为 html)
  @Autowired
  private AjaxLogoutSuccessHandler logoutSuccessHandler;  //注销成功返回的 JSON 格式数据给前端(否则为 登录时的 html)
  @Autowired
  private AjaxAccessDeniedHandler accessDeniedHandler;  //无权访问返回的 JSON 格式数据给前端(否则为 403 html 页面)

  @Autowired
  private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;    // JWT 拦截器

  @Autowired
  private CustomAuthenticationProvider authenticationProvider;

  @Autowired
  private AuthenticationDetailsSource<HttpServletRequest, WebAuthenticationDetails> authenticationDetailsSource;

  /**
   * 配置用户信息,密码加密方式
   *
   * @param auth
   * @throws Exception
   */
  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    // auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
    auth.authenticationProvider(authenticationProvider);
  }


  @Override
  public void configure(WebSecurity web) throws Exception {

    web.ignoring().antMatchers(AUTH_WHITELIST);
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    // 去掉 CSRF
    http.csrf().disable()
        .sessionManagement()
        .sessionCreationPolicy(SessionCreationPolicy.STATELESS) // 使用 JWT,关闭token
        .and()

        .httpBasic().authenticationEntryPoint(authenticationEntryPoint)

        .and()
        .authorizeRequests()
        .antMatchers("/index.html").permitAll()
        .anyRequest()//任何请求,登录后可以访问
        .access("@rbacauthorityservice.hasPermission(request,authentication)") // RBAC 动态 url 认证

        .and()
        .formLogin()
        .successHandler(authenticationSuccessHandler)
        .failureHandler(authenticationFailureHandler)
        .permitAll()
        .authenticationDetailsSource(authenticationDetailsSource)

        .and()
        .logout()
        .logoutUrl("/logout")
        .logoutSuccessHandler(logoutSuccessHandler)
        .permitAll();

    http.exceptionHandling().accessDeniedHandler(accessDeniedHandler);
    http.addFilterBefore(jwtAuthenticationTokenFilter,
        UsernamePasswordAuthenticationFilter.class);

  }


}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值