依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
补充:
引入以后就能用基本的验证。密码在控制台,账户默认user;
关键接口
public class SecurityConfig extends WebSecurityConfigurerAdapter {
关键方法
方法1:用于配置和新增账户
// 配置账户方法
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//配置一个新账户,配置密码,并且设置一个匹配的路径
auth.inMemoryAuthentication().withUser("user_admin").password("8888888").authorities("/");
}
方法2:用于配置访问权限
// 配置拦截规则,认证方式的方法
@Override
protected void configure(HttpSecurity http) throws Exception {
// 配置所有路径都需要验证,并且 and指定 使用 httpbasic 验证
http.authorizeRequests().antMatchers("/**").fullyAuthenticated().and().httpBasic();
}
然后我们需要配置一个加密bean
/**
* 加密bean
*/
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public static NoOpPasswordEncoder passwordEncoder(){
return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
}
不然会提示没有配置加密方式
验证时会到内存中对身份进行验证,验证通过以后就会生成一个sessionId返回给浏览器客户端,cookie
这就是基本的方式实现(没啥人用这个了)