使用Spring Security的OAuth2进行登录和注销

让我们使用OAuth进行身份验证。

这篇文章讲述了如何使用OAuth进行认证。虽然OAuth通常用于授权,但实际上它在很大程度上被用于身份验证。
简单来说,OAuth的授权就像是交给别人家的备用钥匙。如果你有这把钥匙,你就可以进入别人家里。但是,这把钥匙的持有者不一定是家的主人。如果这把钥匙被盗了,其他人就能进入家里。
在OAuth中,这把钥匙被称为”AccessToken”,通过传递这个AccessToken,也就是进行AccessToken的发行,你就可以进入别人家里,也就是”可以使用该服务的信息”。
本文的目的是通过利用这个机制来建立对拥有这个AccessToken的人的信任,以便可以使用该服务。本篇文章仅解释了客户端的处理部分。

我们现在立即使用Spring来创建一个使用这个机制的Web服务吧。

创建项目

春季初始化器

2018-03-05.png

我們將使用SPRING INITIALIZR來創建項目。基本上我們將使用圖像設置來創建Gradle項目,然後在Eclipse等開發工具中導入並進行開發。依賴項目至少需要Web、Thymeleaf、Spring Security和Devtool。我們將使用Spring Boot版本v1.5(請注意,在2.0版本中,實現方法有所不同)。

Slack 应用程序接口

我们将使用Slack作为OAuth认证的服务。似乎还可以使用Google、Facebook、Github等其他选项。

    Slack API

你们可以自行决定应用程序的名称。
请不要忘记将重定向目标设置为http://localhost:8080/。

写代码

    build.gradle

当您使用SPRING INITIALIZR创建项目时,

compile('org.springframework.security.oauth:spring-security-oauth2')

因为之前没有添加,所以我加上了。

buildscript {
    ext {
        springBootVersion = '1.5.10.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-security')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.security.oauth:spring-security-oauth2')  // 追加
    runtime('org.springframework.boot:spring-boot-devtools')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('org.springframework.security:spring-security-test')
}
    設定ファイル

请从Slack API获取${Client ID}和${Client Secret},并将其放置在单引号中以替换。

security:
  oauth2:
    client: 
      clientId: '${Client ID}' # ここを変更、Slackのアプリケーション登録で表示された「Client ID」
      clientSecret: '${Client Secret}' # ここを変更、Slackのアプリケーション登録で表示された「Client Secret」
      accessTokenUri: https://slack.com/api/oauth.access # Slackを利用する場合の設定値
      userAuthorizationUri: https://slack.com/oauth/authorize # Slackを利用する場合の設定値
      authenticationScheme: query # Slackを利用する場合の設定値
      scope: identify # Slackを利用する場合の設定値
      tokenName: token # Slackを利用する場合の設定値
    resource: 
      userInfoUri: https://slack.com/api/auth.test # Slackを利用する場合の設定値
  basic:
    enabled: false
    Controller

仅实现对TOP页面和登录后的页面的映射

package com.example.Security.OAuth2.test.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MainController {
    /**
     * Topページ
     * @return
     */
    @RequestMapping("/")
    public String index() {
        return "index";
    }


    /**
     * ログイン後のページ
     * @return
     */
    @RequestMapping("/hoge")
    public String hoge() {
        return "hoge";
    }
}
    Config

本来应该启用CSRF设置,但由于这次是测试环境,所以已经禁用了。

package com.example.Security.OAuth2.test.config;

import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

@EnableWebSecurity
@EnableOAuth2Sso
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(WebSecurity web) throws Exception {
        // セキュリティ設定を無視するリクエストの設定
        // 静的リソース(images、css、javascript)を無視する設定を記述
        web.ignoring().antMatchers(
                            "/images/**",
                            "/css/**",
                            "/javascript/**",
                            "/webjars/**");
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable() // CSRF対策を無効化
            .authorizeRequests()
                // indexはアクセスを許可
                .antMatchers("/").permitAll()
                .anyRequest().authenticated()
            .and()
                .logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout**"))       // ログアウト処理のパス
                .logoutSuccessUrl("/")
                // ログアウト時に削除するクッキー名
                .deleteCookies("JSESSIONID")
                // ログアウト時のセッション破棄を有効化
                .invalidateHttpSession(true)
                .permitAll();

    }

}
    html files

只需描述登录和登出的转换页面。
似乎登出需要使用POST方式发送。(因为隐藏字段会将CSRF值一起发送?)

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Security OAuth2 Test</title>
</head>
<body>
<h1>Hello, Index</h1>

<a href="/hoge">Login</a>

</body>
</html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Security OAuth2 Test</title>
</head>
<body>
<h1>Hello, Hoge</h1>

<form action="#" th:action="@{/logout}" method="post">
    <button type="submit" value="POST" >Logout</button>
</form>

</body>
</html>

我已经在GitHub上公开了这次的源代码。Spring-OAuth2。

请参考以下网站

    Spring BootでOAuthログインとAPIへのアクセス制限を実現する
bannerAds