【问题标题】:Spring MVC + Java Config + Embedded Tomcat - Spring Security 3 Not WorkingSpring MVC + Java Config + Embedded Tomcat - Spring Security 3 不工作
【发布时间】:2016-05-30 21:28:32
【问题描述】:

围绕这些关键字有很多答案,但似乎没有什么能解决我的问题。我可能没有“明白”,但目前我从其他解决方案中了解到的任何东西都没有在我的特定情况下起作用。

由于我们使用的部署方法,我不得不取消 Spring Bootify 应用程序,这意味着显式设置 Tomcat 并处理 Spring Boot 为您处理的所有小烦恼。

我对此感兴趣的依赖项是:

spring (core, etc) 3.2.13.RELEASE
spring-security (core, web, config) 3.2.9.RELEASE
tomcat-embed-core 7.0.67

我在我的主要方法中设置了 Tomcat,我还设置了所有上下文:

final Tomcat tomcat = new Tomcat();
tomcat.setPort(serverPort);

final Context container = tomcat.addContext(contextRootPath, staticFileDocumentBaseDirectoryAbsolutePathStr);

final AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.setDisplayName("rootContext");
rootContext.register(CommonBeans.class);
rootContext.register(ApplicationBeans.class);

final AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.setDisplayName("dispatcherContext");
dispatcherContext.register(MvcConfig.class);
dispatcherContext.scan("app.configuration.controllers");

final DispatcherServlet dispatcherServlet = new DispatcherServlet(dispatcherContext);

final Wrapper wrapper = Tomcat.addServlet(container, APPLICATION_NAME, dispatcherServlet);
wrapper.setLoadOnStartup(1);

container.addServletMapping("/", APPLICATION_NAME);
tomcat.start();

我的 MVCConfig 类导入 SecurityConfiguration 类

@Configuration
@EnableWebMvc
@Import({ SecurityBeans.class, SecurityConfiguration.class, ThymeleafConfig.class })
public class MvcConfig extends WebMvcConfigurerAdapter {
    addResourceHandlers().......
}

我的安全配置如下:

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired(required = true)
    @Qualifier("realAuthProvider")
    private AuthenticationProvider authenticationProvider;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProvider);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProvider);
    }

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                .authorizeRequests()
                .antMatchers("/css/**", "/images/**", "/js/**", "/fonts/**", "/less/**", "/webjars/**", "/login", "/logout", "/404", "/403", "/formlyHtml/**").permitAll()
                .antMatchers("/**").authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .failureUrl("/login?error=bad_credentials")
                .defaultSuccessUrl("/")
                .and()
                .exceptionHandling().accessDeniedPage("/403")
                .and()
                .logout()
                .invalidateHttpSession(true)
                .deleteCookies("JSESSIONID")
                .logoutUrl("/logout")
                .logoutSuccessUrl("/")
                .and()
                .csrf().disable();
    }
}

现在,我希望我的控制器,映射到“/”需要身份验证,Spring Security 应该重定向到“登录”,在那里我漂亮可爱的登录页面将显示,但它不会重定向,它直接进入“/”控制器,该控制器触发一个空指针,因为上下文中的经过身份验证的用户不存在。

我已经阅读了有关 SecurityConfig 可能未分配给正确上下文的信息,但是查看我的主要 Tomcat 设置,我认为我会在那里正确处理这个问题。唯一的混合可能是因为 Mvc 控制器需要从应用程序 bean(CommonBeans 和 ApplicationBeans)自动装配的依赖项,所以我需要在它们的 @Controller 注释之后导入这些类。

我还有一个类 - SpringSecurityInitializer 扩展 AbstractSecurityWebApplicationInitializer - 与 MvcConfig 位于同一个包中,但这似乎没有被加载(但我不知道在我的配置中会引用什么魔法)

有人能告诉我为什么我的 Spring Security 配置似乎无法与我的控制器一起运行吗?

谢谢

【问题讨论】:

  • 这个问题你解决了吗?我现在也有同样的问题,我真的不知道该怎么办

标签: java spring-mvc tomcat spring-security spring-java-config


【解决方案1】:

您是否初始化了 Spring 安全性?

你需要创建一个extendsAbstractSecurityWebApplicationInitializer的类。

这会自动加载 springSecurityFilterChain,并注册 DelegatingFilterProxy 以在任何其他注册的过滤器之前使用 springSecurityFilterChain(基本上,它会将 Spring 安全性“阻碍”您的请求):

public class SecurityInitializer extends AbstractSecurityWebApplicationInitializer {}

我也会删除:

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.authenticationProvider(authenticationProvider);
}

您已经在配置中的覆盖方法中配置了authenticationProvider

有关示例,请参阅 here

【讨论】:

  • 您的链接返回的 HTTP 404 页面未找到
猜你喜欢
  • 1970-01-01
  • 2013-10-13
  • 2011-09-19
  • 2017-01-09
  • 1970-01-01
  • 1970-01-01
  • 2023-02-04
  • 2012-04-18
  • 2018-07-13
相关资源
最近更新 更多