如何将Spring Boot配置为.war文件以及安全性?

SpringBoot" title="SpringBoot">SpringBootServletInitializer类开始我的Spring Boot应用程序,因为我想将它部署为.war文件。我不知道如何启用安全性。如何将Spring Boot配置为.war文件以及安全性?

public class ApplicationInitializer extends SpringBootServletInitializer { 

private static final Object[] configurations = {

SecurityConfiguration.class,

ApplicationInitializer.class

};

@Override

protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

return application.sources(configurations);

}

}

其中SecurityConfiguration情况如下:

@Configuration 

@EnableWebSecurity

public class SecurityConfiguration extends WebSecurityConfigurerAdapter{

@Override

protected void configure(AuthenticationManagerBuilder auth) throws Exception {

/******/

}

@Override

protected void configure(HttpSecurity http) throws Exception {

/******/

}

}

有了这个配置,我得到AlreadyBuiltException:

'springSecurityFilterChain' threw exception; nested exception is org.springframework.security.config.annotation.AlreadyBuiltException: This object has already been built 

回答:

错就错在SecurityConfiguration代替:

@Override 

protected void configure(AuthenticationManagerBuilder auth) throws Exception {

/******/

}

应该是:

@Autowired 

public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

auth.inMemoryAuthentication()

.withUser("user").password("password").roles("USER");

}

但我不知道为什么这应该写这种方式。 :/

回答:

使用@EnableGlobalAuthentication用于指示类可以被用来配置AuthenticationManagerBuilder

@Configuration 

@EnableWebSecurity

@EnableGlobalAuthentication

public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Autowired

public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

//

}

@Override

protected void configure(HttpSecurity http) throws Exception {

//

}

}

以上是 如何将Spring Boot配置为.war文件以及安全性? 的全部内容, 来源链接: utcz.com/qa/262506.html

回到顶部