一、Spring Boot 基本配置
1.添加 依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
2.添加测试方法
@RestController public class SecurityController { @RequestMapping("/test") public String securityTest() { return "test"; } }
3.启动项目进行访问
系统产生随机的密码
默认用户名为user ,登录成功后
4.配置用户名和密码
spring.security.user.password=kxg11 spring.security.user.name=kxg spring.security.user.roles=admin//用户角色
5.基于内存的认证
@Configuration public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter { @Bean PasswordEncoder passwordEncoder() { return NoOpPasswordEncoder.getInstance(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("admin").password("123").roles("ADMIN","USER") .and() .withUser("san").password("123").roles("USER"); } }
6.HttpSecuity
@Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/admin/**") .hasR