我已經使用Spring Initializr,使用嵌入式Tomcat + Thymeleaf模板引擎生成了一個Spring Boot Web應用程序,並將其作爲可執行JAR文件打包。使用Spring Boot - Spring security @ComponentScan或@Import
技術:
春季啓動1.4.2.RELEASE,春天4.3.4.RELEASE,Thymeleaf 2.1.5.RELEASE,Tomcat的嵌入8.5.6時,Maven 3,Java的8
我有此安全類
com.tdk.config
/**
* @author nunito
* @version 1.0
* @since 4 mar. 2017
*/
@Configuration
@EnableWebSecurity
@PropertySource("classpath:/com/tdk/config/app-${APP-KEY}.properties")
public class SecurityConfig extends WebSecurityConfigurerAdapter {
protected String loginPage = "/tdk/login";
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
.loginPage(getLoginPage())
.permitAll()
.and()
.authorizeRequests()
.antMatchers("/mockup/**").permitAll()
.antMatchers("/welcome/**").authenticated()
.and()
.logout()
.permitAll()
.logoutSuccessUrl("/index.html");
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.passwordEncoder(new StandardPasswordEncoder())
.withUser("nunito").password("08c461ad70fce6c74e12745931085508ccb2090f2eae3707f6b62089c634ddd2636f380f40109dfb").roles("ADMIN").and()
.withUser("nunito").password("4cfbf05e4493d17125c547fdba494033d7aceee9310f253f3e96c4f928333d2436d669d63a84fe4f").roles("ADMIN");
}
public String getLoginPage() {
return loginPage;
}
public void setLoginPage(String loginPage) {
this.loginPage = loginPage;
}
使用這個配置文件
@SpringBootApplication
@ComponentScan(basePackages = "com.tdk.config")
@EnableAutoConfiguration
public class TdkCloudApplication {
public static void main(String[] args) {
SpringApplication.run(TdkCloudApplication.class, args);
}
}
我有一個404,無論我存取權限S IN的URL
但這種配置一切正常
@SpringBootApplication
@EnableAutoConfiguration
@Import({SecurityConfig.class})
public class TdkCloudApplication {
public static void main(String[] args) {
SpringApplication.run(TdkCloudApplication.class, args);
}
}
我想知道其中的差別,因爲對我來說是兩種不同的方式做同樣認爲
您可以嘗試刪除'@EnableWebSecurity',因爲它禁用了spring-boot自動配置,因爲不需要'@ComponentScan',因爲spring引導會掃描所有子包,不需要'@EnableAutoConfiguration' – Shahbour