質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

0回答

3836閲覧

ログインしているユーザー情報を得たい

Yoshi--

総合スコア62

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2017/07/21 06:33

編集2017/07/24 05:15

html

1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4 <meta charset="utf-8"/> 5 <title>婚活サイト</title> 6 <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" 7 th:href="@{/webjars/bootstrap/3.3.7/css/bootstrap.min.css}"/> 8 <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" 9 th:href="@{/webjars/bootstrap/3.3.7/css/bootstrap-theme.min.css}"/> 10</head> 11 12<body th:with="user=${#authentication.principal.user}"> 13 <!-- authenticationで認証ユーザー情報にアクセスできる 、principalプロパティでUserDetailsオブジェクトにアクセスできるので、user.mailでログインユーザー名が分かる--> 14 15 <h2> 16 <span th:text="${user.mail}">メアド</span> 17 さん、Myプロフィール 18 </h2> 19 20 21 <table class="table table-striped table-bordered table-condensed"> 22 23 <tr> 24 <th>写真</th> 25 <th>名前</th> 26 <th>性別</th> 27 <th>誕生日</th> 28 <th>身長(cm)</th> 29 <th>職業</th> 30 <th>年収(万円)</th> 31 <th>メールアドレス</th> 32 <th>自己紹介</th> 33 </tr> 34 35 <tr> 36 <!-- エンコードした画像の表示 --> 37 <td><img th:src="${'data:image/png;base64,' + profile.image}" width="250" height="150"/></td> 38 <td th:text="${profile.name}">山田</td> 39 <td th:text="${profile.gender}"></td> 40 <td th:text="${profile.birthday}">誕生日</td> 41 <td th:text="${profile.height}">170</td> 42 <td th:text="${profile.occupationName}">職業</td> 43 <td th:text="${profile.income}">年収</td> 44 <td th:text="${profile.mail}">メール</td> <!-- 「profile」オブジェクトがもつ「user」の「mail」を表示 --> 45 <td th:text="${profile.text}">自己紹介</td> 46 47 </tr> 48 </table> 49 50 <form th:action="@{/konkatsu/usersList}" method="get"> 51 <input type="hidden" name="genderId" th:value="${profile.genderId}" /> 52 <button type="submit" class="btn btn-lg btn-primary btn-block">登録ユーザー一覧を見る</button> 53 </form> 54 55 <form th:action="@{/logout}" method="post"> 56 <input type="submit" class="btn btn-lg btn-primary btn-block" value="ログアウト" /> 57 </form> 58 59</body> 60</html>

body th:with="user=${#authentication.principal.user}"> <h2> <span th:text="${user.mail}">メアド</span> さん、Myプロフィール </h2>

みたいにログインユーザーの情報を得たいです

メールアドレスは取得できますが
プロフィールのGENDER_IDを取得したいです
Users、profileテーブルはjoinしてあります

イメージ説明

LoginUserDetails.java

java

1//UserDetailsの実装クラス 2 3package com.example.konkatsu.service; 4 5import org.springframework.security.core.authority.AuthorityUtils; 6 7import com.example.konkatsu.domain.User; 8 9import lombok.Data; 10 11@Data 12public class LoginUserDetails extends org.springframework.security.core.userdetails.User{ 13 private final User user; 14 15 16 public LoginUserDetails(User user){ 17 super(user.getMail(), user.getPass(), AuthorityUtils.createAuthorityList("ROLE_USER")); 18 this.user = user; 19 } 20} 21

SecurityConfig.java

java

1 2 3package com.example.konkatsu; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.context.annotation.Bean; 7import org.springframework.context.annotation.Configuration; 8import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 9import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter; 10import org.springframework.security.config.annotation.web.builders.HttpSecurity; 11import org.springframework.security.config.annotation.web.builders.WebSecurity; 12import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 13import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 14import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 15import org.springframework.security.crypto.password.PasswordEncoder; 16 17import com.example.konkatsu.service.LoginUserDetailsService; 18 19@EnableWebSecurity //Spring Securityの基本的な設定(認証フィルタの設定など)が行われる 20public class SecurityConfig extends WebSecurityConfigurerAdapter { //WebSecurityConfigurerAdapter を継承することでデフォルト設定に対して追加したい箇所だけオーバーライドして設定できる 21 22 @Override 23 public void configure(WebSecurity web) throws Exception { //configure(WebSecurity)メソッドをオーバーライドすることで特定のリクエストに対してセキュリティー設定を無視する設定など、全体に関わる設定ができる 24 web.ignoring().antMatchers("/webjars/**", "/css/**"); //「/webjars」「/css」といった静的リソースに対するアクセスにはセキュリティの設定は無視するようにする 25 } 26 27 @Override 28 protected void configure(HttpSecurity http) throws Exception { //configure(HttpSecurity)メソッドをオーバーライドすることで、認可の設定やログイン、ログアウトに関する設定ができる 29 http 30 .authorizeRequests() //認証が必要となるURLを設定 31 .antMatchers("/login").permitAll() //「/login」URLは認識不要。任意のユーザーがアクセスできるようにする 32 .antMatchers("/RegisterUser").permitAll() 33 .antMatchers("/createUser").permitAll() 34 .antMatchers("/createProfile").permitAll() 35 // .antMatchers("/konkatsu").permitAll() 36 .antMatchers("/RegisterUserForm").permitAll() //ユーザー登録は認識不要 37 .anyRequest().authenticated() //それ以外のパスには、認証なしでアクセスできないようにする 38 39 40 41 .and() 42 .formLogin().loginProcessingUrl("/login") //ログインに関する設定 43 .loginPage("/login") //ログインフォーム表示のパス(URL) 44 .failureUrl("/login?error") //認証失敗時の遷移先(?errorとつけておくとthymeleafの方でエラーのメッセージを出すときに便利) 45 .defaultSuccessUrl("/konkatsu", true) /*認証成功時の遷移先(第2引数のboolean 46 true : ログイン画面した後必ずtopにとばされる 47 false : (認証されてなくて一度ログイン画面に飛ばされても)ログインしたら指定したURLに飛んでくれる) */ 48 .usernameParameter("mail").passwordParameter("pass") //メールアドレス、パスワードのパラメータ名を設定(ログイン画面のhtmlのinputのname属性を見に行っている) 49 .and() 50 .logout() 51 .logoutSuccessUrl("/logout"); 52 53 } 54 55 @Bean 56 PasswordEncoder passwordEncoder() { 57 return new BCryptPasswordEncoder(); //パスワードをハッシュ化する 58 } 59 60 61 /*このクラスは認証処理時に自動で呼ばれるクラス 62 やっていることは入力されたパスワードに対してBCryptでハッシュ化し、入力値が正しいか認証を行っている*/ 63 @Configuration 64 protected static class AuthenticationConfiguration 65 extends GlobalAuthenticationConfigurerAdapter { 66 @Autowired 67 LoginUserDetailsService userDetailsService; 68 69 @Override 70 public void init(AuthenticationManagerBuilder auth) throws Exception { 71 auth.userDetailsService(userDetailsService) 72 .passwordEncoder(new BCryptPasswordEncoder()); 73 } 74 } 75} 76

ログインしているusersテーブルは
"${user.mail}"で取得できるので

やりたいことは
ログインしているユーザーの
プロフィールの情報を同じように得たいです

Securityのところで何か紐付けが必要なのでしょうか??

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

A-pZ

2017/07/24 04:40

SpringSecurity関連の設定がないので、SpringSecurityに関する設定やLoginUserDetailsに関する設定があると回答が得られるでしょう。(通常の設定では取得できません)
Yoshi--

2017/07/24 05:18 編集

ありがとうございます。追加いたしました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問