Spring Security与Spring Boot集成
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/css/**", "/js/**", "/fonts/**", "/index").permitAll()
.antMatchers("/users/**").hasRole("ADMIN")
.and()
.formLogin() // 基于form表单登录验证
.loginPage("/login").failureUrl("/login-error"); // 自定义登录界面
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication() // 认证信息存储与内存中
.withUser("vincent")
.password("123456")
.roles("ADMIN");
}
}