###実現したいこと
Spring BootでOAuth2を使って認証しようとしています。
Googleでの認証は出来たのですが、そのあとのユーザ識別やログイン状態の保持方法が分かりません。
###困っていること、分からないこと
- ログイン状態の保持期間を1時間とかにしたい
(GoogleのOAuth2の情報は30分でクリアされるようなので)
- ユーザを一意に特定するのはOAuth2Authentication.getUserAuthentication().getDetails().get("sub")でよいか
(RootController.java 参照)
- ログイン直後にユーザに関する情報をセッションに入れるタイミングがほしい
(onLogin()をオーバーライドするイメージ)
- Google以外でもOAuthで認証するときに困らないような書き方
###関連ソース
- RootController.java
Java
1@Controller 2@Slf4j 3public class RootController { 4 @RequestMapping("/") 5 public String index(Model model, HttpServletRequest request, OAuth2Authentication auth) { 6 if (request.isUserInRole("ROLE_USER")) { 7 model.addAttribute("user", request.getRemoteUser()); 8 Object authDetails = auth.getUserAuthentication().getDetails(); 9 Map<?, ?> mm = null; 10 if (authDetails instanceof LinkedHashMap) { 11 mm = (LinkedHashMap<?, ?>) authDetails; 12 } 13 model.addAttribute("sub", mm.get("sub")); 14 model.addAttribute("email", mm.get("email")); 15 16 log.info("logined:{}", mm.get("email")); 17 for (Entry<?, ?> m : mm.entrySet()) { 18 log.info("AUTH:{}={}", m.getKey(), m.getValue()); 19 } 20 } else { 21 log.info("no login"); 22 } 23 24 if (auth == null) { 25 log.info("auth:null"); 26 } else { 27 log.info("name:{}", auth); 28 } 29 30 return "index"; 31 } 32}
- SecurityConfig.java
Java
1@Component 2@EnableOAuth2Sso 3public class SecurityConfig extends WebSecurityConfigurerAdapter { 4 @Override 5 public void configure(HttpSecurity http) throws Exception { 6 http 7 .authorizeRequests().antMatchers("/").permitAll().anyRequest().authenticated() 8 .and().logout().logoutSuccessUrl("/").permitAll(); 9 } 10}
- application.yml
YAML
1security: 2 oauth2: 3 client: 4 clientId: xxx.apps.googleusercontent.com 5 clientSecret: yyy 6 accessTokenUri: https://www.googleapis.com/oauth2/v4/token 7 userAuthorizationUri: https://accounts.google.com/o/oauth2/v2/auth 8 clientAuthenticationScheme: form 9 scope: 10 openid 11 email 12 resource: 13 userInfoUri: https://www.googleapis.com/oauth2/v3/userinfo 14 preferTokenInfo: true 15
- build.gradle
Gradle
1buildscript { 2 ext { 3 springBootVersion = '1.5.3.RELEASE' 4 } 5 repositories { 6 mavenCentral() 7 } 8 dependencies { 9 classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 10 } 11} 12 13apply plugin: 'java' 14apply plugin: 'eclipse-wtp' 15apply plugin: 'org.springframework.boot' 16apply plugin: 'war' 17 18war { 19 baseName = 'xxx' 20 version = '0.0.1' 21} 22 23sourceCompatibility = 1.8 24 25repositories { 26 mavenCentral() 27} 28 29configurations { 30 providedRuntime 31} 32 33dependencies { 34 compile('org.springframework.boot:spring-boot-configuration-processor') 35 compile('org.springframework.boot:spring-boot-starter-aop') 36 compile('org.springframework.boot:spring-boot-starter-data-jpa') 37 compile('org.springframework.boot:spring-boot-starter-security') 38 compile('org.springframework.boot:spring-boot-starter-thymeleaf') 39 compile('org.springframework.boot:spring-boot-starter-web') 40 compile('org.springframework.security.oauth:spring-security-oauth2') 41 compileOnly('org.projectlombok:lombok') 42 runtime('org.springframework.boot:spring-boot-devtools') //コード更新時にアプリケーションをリロード 43 runtime('org.postgresql:postgresql') 44 providedRuntime('org.springframework.boot:spring-boot-starter-tomcat') 45 testCompile('org.springframework.boot:spring-boot-starter-test') 46} 47 48bootRun { 49 jvmArgs = ['-Dspring.output.ansi.enabled=always'] //コンソールに色を付ける 50}
###補足情報
開発環境:Eclipse 4.6 (Pleiades)
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/29 03:04
2017/06/29 11:37