最近在实习中,需要用到Oauth协议来进行资源的认证,所以在此记录一下,让有需要的人少走一些弯路。(PS:Google真是个好东西,Github和官网也是好东西,虽然英文看起来挺费劲的)
这是官方实例:
认证服务器:https://github.com/spring-cloud-samples/authserver
SSO客户端:https://github.com/spring-cloud-samples/sso
要了解Oauth我们首先需要实现登录,这里使用SpringSecurity
刚接触Springboot和Security的人可能会比较难理解,所以我一步步来,编辑器使用的IDEA:
关于Spring Security 最好还是看官网说明
https://docs.spring.io/spring-security/site/docs/5.0.0.RELEASE/reference/htmlsingle/#pe-dpe
使用Security进行简单的密码登录验证
首先创建一个Springboot项目,引入Maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
我们新建一个Config文档用来放配置类
新建SecurityConfig,使其继承WebSecurityConfigurerAdapter,并使用@EnableWebSecurity 开启Spring Security
@Configuration
@EnableWebSecurity //开启Spring Security
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//加上这个方法就是不去管方法是否已被弃用
@SuppressWarnings("deprecation")
@Bean
public static NoOpPasswordEncoder passwordEncoder() {
return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
}
@Override