围绕这些关键字的答案很多,但似乎没有解决我的问题。我可能不会得到它'但是目前我从其他解决方案中理解的并不是我在特定情况下的工作。
由于部署方法,我们不得不使用Spring Spring启动应用程序,这意味着明确设置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应该重定向到" login",这里会显示我漂亮可爱的登录页面,但它不会重定向,它只是直接进入" /&# 34;控制器,它会触发空指针,因为上下文中的经过身份验证的用户不存在。
我已经阅读过有关SecurityConfig的内容可能没有被分配到正确的上下文,但是看看我的主要Tomcat设置我认为我会在那里正确处理这个问题。唯一的混合可能是因为Mvc控制器需要从应用程序bean(CommonBeans和ApplicationBeans)自动装配的依赖项,所以我需要在它们的@Controller注释之后导入这些类。
我也有一个类 - SpringSecurityInitializer扩展AbstractSecurityWebApplicationInitializer - 与MvcConfig位于同一个包中但是这似乎没有加载(但是我不知道将要引用哪个魔法)我的配置)
有人可以告诉我为什么我的Spring Security配置似乎不能根据我在这里得到的控制器运行吗?
由于
答案 0 :(得分:0)
您是否已初始化Spring安全性?
您需要创建一个extends
AbstractSecurityWebApplicationInitializer。
这会自动加载springSecurityFilterChain,并在任何其他已注册的Filter之前注册DelegatingFilterProxy
以使用springSecurityFilterChain
(基本上,它会将Spring安全性和#34;放在你的方式中#34;请求):
public class SecurityInitializer extends AbstractSecurityWebApplicationInitializer {}
我也会删除:
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider);
}
您已在配置中的重写方法中配置authenticationProvider
。
有关示例,请参阅here。