在本教程中,咱们将讨论如何使用Spring Security OAuth和Spring Boot实现SSO - 单点登陆。html
很是简单地说,当用户试图访问客户端应用程序中的安全页面时,他们将被重定向到首先经过身份验证服务器进行身份验证。前端
咱们将使用OAuth2中的受权代码受权类型来驱动身份验证委派。git
让咱们从客户端应用程序开始;固然,咱们将使用Spring Boot来最小化配置:github
首先,咱们须要在pom.xml中使用如下依赖项:web
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.security.oauth.boot</groupId> <artifactId>spring-security-oauth2-autoconfigure</artifactId> <version>2.0.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity4</artifactId> </dependency>
接下来,最重要的部分,咱们的客户端应用程序的Security配置:spring
@Configuration @EnableOAuth2Sso public class UiSecurityConfig extends WebSecurityConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http.antMatcher("/**") .authorizeRequests() .antMatchers("/", "/login**") .permitAll() .anyRequest() .authenticated(); } }
固然,这种配置的核心部分是咱们用于启用单点登陆的@ EnableOAuth2Sso注释。安全
请注意,我们须要扩展WebSecurityConfigurerAdapter - 若是没有它,全部路径都将受到保护 - 所以用户将在尝试访问任何页面时重定向以登陆。在咱们的例子中,首页和登陆页面是惟一能够在没有身份验证的状况下访问的页面。服务器
最后,咱们还定义了一个RequestContextListener bean来处理请求范围。cookie
server: port: 8082 servlet: context-path: /ui session: cookie: name: UISESSION security: basic: enabled: false oauth2: client: clientId: SampleClientId clientSecret: secret accessTokenUri: http://localhost:8081/auth/oauth/token userAuthorizationUri: http://localhost:8081/auth/oauth/authorize resource: userInfoUri: http://localhost:8081/auth/user/me spring: thymeleaf: cache: false
另请注意,在咱们的示例中,咱们推出了受权服务器,但固然咱们也可使用其余第三方提供商,如Facebook或GitHub。session
如今,让咱们来看看客户端应用程序的前端配置。咱们不会在这里专一于此,主要是由于咱们已经在网站上介绍过。
咱们的客户端应用程序有一个很是简单的前端;这是index.html:
<h1>Spring Security SSO</h1> <a href="securedPage">Login</a>
和securedPage.html:
<h1>Secured Page</h1> Welcome, <span th:text="${#authentication.name}">Name</span>
securedPage.html页面须要对用户进行身份验证。若是未经身份验证的用户尝试访问securedPage.html,则会首先将其重定向到登陆页面。
如今让咱们在这里讨论咱们的受权服务器。
首先,咱们须要在pom.xml中定义依赖项:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>2.3.3.RELEASE</version> </dependency>
重要的是要理解咱们将在这里一块儿运行受权服务器和资源服务器,做为单个可部署单元。
让咱们从资源服务器的配置开始 :
@SpringBootApplication @EnableResourceServer public class AuthorizationServerApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(AuthorizationServerApplication.class, args); } }
而后,咱们将配置咱们的受权服务器:
@Configuration @EnableAuthorizationServer public class AuthServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired private BCryptPasswordEncoder passwordEncoder; @Override public void configure( AuthorizationServerSecurityConfigurer oauthServer) throws Exception { oauthServer.tokenKeyAccess("permitAll()") .checkTokenAccess("isAuthenticated()"); } @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("SampleClientId") .secret(passwordEncoder.encode("secret")) .authorizedGrantTypes("authorization_code") .scopes("user_info") .autoApprove(true) .redirectUris("http://localhost:8082/ui/login","http://localhost:8083/ui2/login"); } }
请注意咱们如何仅使用authorization_code grant类型启用简单客户端。
另外,请注意autoApprove如何设置为true,以便咱们不会被重定向并手动批准任何范围。
首先,咱们将经过application.properties禁用默认的基自己份验证:
server.port=8081 server.servlet.context-path=/auth
如今,让咱们转到配置并定义一个简单的表单登陆机制:
@Configuration @Order(1) public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.requestMatchers() .antMatchers("/login", "/oauth/authorize") .and() .authorizeRequests() .anyRequest().authenticated() .and() .formLogin().permitAll(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("john") .password(passwordEncoder().encode("123")) .roles("USER"); } @Bean public BCryptPasswordEncoder passwordEncoder(){ return new BCryptPasswordEncoder(); } }
请注意,咱们使用简单的内存中身份验证,但咱们能够简单地将其替换为自定义userDetailsService。
最后,咱们将建立咱们以前在配置中使用的用户端:
@RestController public class UserController { @GetMapping("/user/me") public Principal user(Principal principal) { return principal; } }
固然,这将使用JSON表示返回用户数据。
在本快速教程中,咱们专一于使用Spring Security Oauth2和Spring Boot实现单点登陆。
与往常同样,能够在GitHub上找到完整的源代码。