当前位置: 首页 > 知识库问答 >
问题:

HandlerInterceptorAdapter不能在具有Spring Security的登录上运行

南门鸿畴
2023-03-14

我的拦截器在所有请求中运行,除了登录。

拦截器:

public class MultitenantHandler extends HandlerInterceptorAdapter {

        private static final Logger log = LoggerFactory.getLogger(MultitenantHandler.class);

        @Override
        public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler){

            String origin = req.getHeader("Origin");
            log.debug("Origin: "+origin);

            if (origin == null) {
                origin = "localhost";
            }

            int indexDot = origin.indexOf(".");
            int indexDash = origin.indexOf("://") + 3;

            String tenant = "";

            if (indexDot == -1) {
                tenant = "experter";
                log.warn("Using default tenant");
                TenantContext.setCurrentTenant(tenant);

            } else {
                tenant = origin.substring(indexDash, indexDot);
                log.info("Using tenant: " + tenant);
                TenantContext.setCurrentTenant(tenant);
            }

            return true;

        }
}

WebMvcConfigrerAdapter上,我这样注册:

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MultitenantHandler());
    }

这是我的安全配置:

@Configuration
@EnableWebSecurity
@Profile({"development", "demo", "default"})
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private final Logger log = LoggerFactory.getLogger(SecurityConfig.class);

    @Autowired
    private CustomUserDetailsService customUserDetailsService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private RESTAuthenticationEntryPoint authenticationEntryPoint;

    @Autowired
    private RESTLogoutSuccessHandler logoutSuccessHandler;

    @Autowired
    private JWTAuthenticationFailureHandler authenticationFailureHandler;

    @Autowired
    private JWTAuthenticationSuccessHandler authenticationSuccessHandler;

    @Autowired
    private StatelessAuthenticationFilter statelessAuthFilter;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.csrf().disable()
                .exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);

        http.formLogin().permitAll()
                .successHandler(authenticationSuccessHandler)
                .failureHandler(authenticationFailureHandler);

        http.logout().permitAll()
                .logoutSuccessHandler(logoutSuccessHandler);

        http.addFilterBefore(statelessAuthFilter, UsernamePasswordAuthenticationFilter.class);

        http.authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
                .antMatchers("/v2/api-docs").hasRole("ADMIN")
                .antMatchers("/login").permitAll()
                .antMatchers("/login/changePassword").permitAll()
                .antMatchers("/user/image").permitAll()
                .antMatchers("/social/login/facebook").permitAll()
                .antMatchers("/actuator/**").hasRole("ADMIN")
                .antMatchers("/admin/**").hasRole("ADMIN")
                .antMatchers("/**").hasRole("USER");

        log.info("Configuration of http complete.");
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth)
            throws Exception {
        auth.userDetailsService(customUserDetailsService).passwordEncoder(passwordEncoder);
    }

当我请求/登录拦截器不运行时,在其他请求中甚至没有登录拦截器正常工作。

我需要在任何请求之前执行拦截器,因为我需要根据url请求设置数据库。

如果你需要更多的信息,请告诉我,我可以在这里发布。

共有1个答案

仲孙默
2023-03-14

如果你有同样的问题,我用filter as@M.Deinum sugested解决了这个问题。我使用了与验证身份验证令牌相同的过滤器。

 类似资料:
  • 本文向大家介绍SpringBoot 配合 SpringSecurity 实现自动登录功能的代码,包括了SpringBoot 配合 SpringSecurity 实现自动登录功能的代码的使用技巧和注意事项,需要的朋友参考一下 自动登录是我们在软件开发时一个非常常见的功能,例如我们登录 QQ 邮箱: 很多网站我们在登录的时候都会看到类似的选项,毕竟总让用户输入用户名密码是一件很麻烦的事。 自动登录功能

  • springsecurity oauth2.0 谁做过记录登录日志?监听事件好像没法区分是什么原因失败的、比如client错误还是用户名错误

  • 问题内容: 我正在尝试使用Windows上的SOnarQube MsBuild在Jenkins的.NET Core 2.0解决方案上启动Sonarqube。 当我从Jenkins工作区执行以下脚本时,它工作正常: 但是,当我从詹金斯执行相同的命令时,我得到了以下消息: SonarQube MSBuild集成失败:SonarQube无法收集有关您的项目的必需信息。可能的原因: 该项目尚未构建-必须在

  • 我正在升级运行在Tomcat 8上的Web应用程序的log4j版本。我正在从1.2.12版本升级到最新的2.11.1。 应用程序正在使用Maven。因此,我向我的应用程序(版本在父pom中管理): 我删除了旧的配置文件,并将其替换为文件夹中的一个新的文件(在我的log4j2文件内容下面找到)。 当我直接运行函数时,日志会正确地写入控制台和文件中。但是,当我在Tomcat上部署应用程序并运行它时,没

  • 问题内容: 我已经在使用基本的日志记录配置,其中所有模块中的所有消息都存储在一个文件中。但是,我现在需要一个更复杂的解决方案: 两个文件:第一个保持不变。 第二个文件应具有一些自定义格式。 我一直在阅读该模块的文档,但目前它们对我来说很复杂。记录器,处理程序… 因此,简而言之: 如何在Python 3中登录到两个文件,即: 问题答案: 您可以执行以下操作:

  • 我安装了最新版本的Java,当我尝试运行eclipse时,它会说: JVM的版本不适合本产品。版本:需要或更高版本。 为什么?我该怎么办?