Spring Security与Spring Boot集成
依赖
<dependency>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-security
</artifactId
>
</dependency
>
如果有用到Thymeleaf模板,导入相关依赖
<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");
}
}
转载请注明原文地址: https://yun.8miu.com/read-59470.html