整合springsecurity依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
创建配置类
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
在配置类中重载方法
//拦截
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/user/getuserinfo").hasRole("0");
http.formLogin();//登录 默认使用login接口
http.logout();//登出 默认使用logout接口
}
//认证
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception{
auth.inMemoryAuthentication()
.passwordEncoder(new BCryptPasswordEncoder())
.withUser("admin")
.password(new BCryptPasswordEncoder().encode("admin")).roles("0")
;
}