• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

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

java 来源:Michał Mielec 4次浏览

由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"); 
} 

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


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)