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

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

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

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

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Q&A

解決済

1回答

6932閲覧

Springsecurityのログイン後の画面について

heavyuseman

総合スコア42

Java

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

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

0グッド

2クリップ

投稿2017/06/10 10:11

編集2017/06/11 10:23

いつもお世話になっております。
Springsecurityのログイン後の画面について質問があります。
ログイン成功した場合、sucess.htmlに画面遷移するように設定しているのですが
first.htmlに設定しているmain.jsのソースの画面が表示されてしまいます。
first.htmlからmain.jsのソースを外し、もう一度ログインを実施したところ
ログイン成功し、sucess.htmlに画面遷移しました。
原因が不明ですのでご教授お願い致します。
2017/06/11追記
一度ログインし、main.jsのソースの画面を表示し、再度First.htmlに戻り
ログインすると、正常にsucess.htmlに画面遷移しました

java

1//Test.java 2package com.tuyano.springboot.app; 3 4import java.util.List; 5 6import org.springframework.beans.factory.annotation.Autowired; 7import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 8import org.springframework.http.MediaType; 9import org.springframework.stereotype.Controller; 10import org.springframework.transaction.annotation.Transactional; 11import org.springframework.ui.Model; 12import org.springframework.web.bind.annotation.ModelAttribute; 13import org.springframework.web.bind.annotation.RequestBody; 14import org.springframework.web.bind.annotation.RequestMapping; 15import org.springframework.web.bind.annotation.RequestMethod; 16import org.springframework.web.bind.annotation.RequestParam; 17import org.springframework.web.bind.annotation.ResponseBody; 18import org.springframework.web.servlet.ModelAndView; 19import com.fasterxml.jackson.databind.SerializationFeature; 20import com.fasterxml.jackson.core.JsonProcessingException; 21import com.fasterxml.jackson.databind.ObjectMapper; 22import com.tuyano.springboot.app.Emp; 23import com.tuyano.springboot.app.EmpRepository; 24import com.tuyano.springboot.dateaccess.DataAccessRepository; 25import com.tuyano.springboot.dateaccess.Newaccount; 26 27 28 29 30@Controller 31@EnableAutoConfiguration 32public class Test { 33 34 @Autowired //リポジトリを紐づけます 35 EmpRepository repository; 36 @Autowired //リポジトリを紐づけます 37 DataAccessRepository dataAccessRepository; 38 @RequestMapping(value="/") 39 @ResponseBody 40 public String home(){ 41 //json値格納変数 42 String jsonvalue; 43 //全件取得します 44 Iterable<Emp> list = repository.findAll(); 45 46 // 取得した内容を出力します 47 for(Emp emp: list){ 48 //Jacksonでオブジェクトを JSON に変換 49 ObjectMapper objectMapper = new ObjectMapper(); 50 try { 51 jsonvalue = objectMapper.writeValueAsString(emp); 52 System.out.println("jsonの値"+jsonvalue); 53 return jsonvalue; 54 }catch (JsonProcessingException e) { 55 e.printStackTrace(); 56 } 57 } 58 return null; 59 60 } 61 //firstGet画面呼び出し 62 @RequestMapping(value="/First",method=RequestMethod.GET) 63 public String FirstGet(){ 64 65 return null; 66 } 67 //firstPost画面呼び出し 68 @RequestMapping(value="/First",method=RequestMethod.POST) 69 public String FirstPost(){ 70 71 return "アイウエオ"; 72 } 73 74 //Sucess画面呼び出し 75 @RequestMapping(value="/sucess",method=RequestMethod.GET) 76 public String SucessGet(){ 77 return null; 78 } 79 //failure画面呼び出し 80 @RequestMapping(value="/failure",method=RequestMethod.GET) 81 public String FailureGet(){ 82 return null; 83 } 84 }

java

1//SecurityConfig.java 2package com.tuyano.springboot.springsecurity; 3 4import org.springframework.context.annotation.Bean; 5import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 6import org.springframework.security.config.annotation.web.builders.HttpSecurity; 7import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 8import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 9import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 10 11import javax.sql.DataSource; 12 13import org.springframework.beans.factory.annotation.Autowired; 14import org.springframework.beans.factory.annotation.Qualifier; 15 16 17 18@EnableWebSecurity 19public class SecurityConfig extends WebSecurityConfigurerAdapter{ 20 @Autowired 21 private DataSource dataSource; 22 23 private static final String USER_QUERY="select name, password, 1 from Newaccount where name = ?"; 24 private static final 25 String ROLES_QUERY="select username, authority from AUTHORITIES where username = ?"; 26 27 @Override 28 protected void configure(HttpSecurity http) throws Exception{ 29 http 30 .authorizeRequests() 31 .antMatchers("/First.html").permitAll() 32 .antMatchers("/templates/**").hasAnyAuthority("ROLE_ADMIN") 33 .anyRequest().authenticated() 34 .and() 35 .formLogin() 36 .loginPage("/First.html") 37 .loginProcessingUrl("/processLogin") 38 .defaultSuccessUrl("/sucess.html") 39 .failureUrl("/failure.html") 40 .usernameParameter("name") 41 .passwordParameter("password") 42 .and() 43 .logout() 44 .logoutUrl("/processLogout") 45 .logoutSuccessUrl("/First") 46 .and() 47 .csrf() 48 .disable(); 49 50 } 51 @Override 52 public void configure(AuthenticationManagerBuilder auth) throws Exception { 53 auth.jdbcAuthentication() 54 .dataSource(dataSource) 55 .usersByUsernameQuery(USER_QUERY) 56 .authoritiesByUsernameQuery(ROLES_QUERY); 57 //passwordEncoder(new BCryptPasswordEncoder()); 58 //.authoritiesByUsernameQuery( 59 // "select mail_address, role from accounts where mail_address = ?"); 60 } 61 62 63} 64

html

1//first.html 2<!DOCTYPE html> 3<html xmlns:th="http://www.thymeleaf.org"> 4<head> 5<title>top page</title> 6<meta http-equiv="Content-Type" 7content="text/html" charset="UTF-8"/> 8 9<!-- css --> 10 11<style> 12 html { height: 100% } 13 body { height: 100%; margin: 0; padding: 0 } 14 #map { height: 100% } 15</style> 16 17</head> 18<body> 19<form action="/processLogin" method="POST"> 20 <dl> 21 <dt> 22 ログイン名前 23 </dt> 24 <dd> 25 <input type="text" name="name"></input> 26</dd> 27 <dt> 28 ログインパスワード 29 </dt> 30 <dd> 31 <input type="password" name="password"></input> 32 </dd> 33 </dl> 34 <button>ログイン</button> 35 </form> 36 <hr/> 37 <form action="urlForUpload" enctype="multipart/form-data" method="post"> 38 <div class="form-group"> 39 <label>■ファイル種類:</label> 40 <select id="select_file_type" name="select_file_type" required=""> 41 <option value="login-user">ログインユーザー</option> 42 <!-- アップロードするファイルを定義していく --> 43 </select> 44 </div> 45 <div class="form-group"> 46 <label>■ファイルパス:</label> 47 <input type="file" id="upload_file" name="upload_file" required="" /> 48 </div> 49 <div class="form-group"> 50 <input id="data_upload_button" type="submit" value="アップロード" /> 51 </div> 52 </form> 53 <table> 54 55</table> 56 <div id ="map"></div> 57<script type="text/javascript" src="js/main.js" th:src="@{/js/main.js}"></script> 58</body> 59</html>

JS

1//main.jsです。ログイン後このソースが画面上に表示されます 2//マップAの緯度経度 3var centerA = { 4lat: 35.66929, // 緯度 5lng: 139.707056 // 経度 6 }; 7 8//マップBの緯度経度 9var centerB_index = [ // マーカーを立てる場所名・緯度・経度 10 { 11 name: '原宿駅', 12 lat: 35.670399, 13 lng: 139.702715 14 //icon: 'tam.png' // TAM 東京のマーカーだけイメージを変更する 15 }, { 16 name: '明治神宮前駅', 17 lat: 35.668486, 18 lng: 139.705376 19 }, { 20 name: '乃木坂駅', 21 lat: 35.66649, 22 lng: 139.726242 23 }, 24 { 25 name: '青山霊園', 26 lat: 35.666929, 27 lng: 139.721562 28 }, { 29 name: '赤坂駅', 30 lat: 35.672212 , 31 lng: 139.73638 32 } 33 ]; 34//マップBのインデックス番号 35var centerB_index_number =centerB_index_i(); 36 37var centerB = centerB_index[centerB_index_number]; 38alert(JSON.stringify(centerB)); 39 40 41 42 43//マップBのインデックス番数を取得 44function centerB_index_i() { 45 var count; 46//for文でハッシュ分回す 47for (var i=0; i < centerB_index.length; i++){ 48 //配列の要素を取得し、緯度の最小値を取得 49 var B_lat_min = Math.min.apply(null,centerB_index.map(function(o){return o.lat;})); 50 //最小値に一致したインデックスを取得 51 if(B_lat_min == centerB_index[i].lat){ 52 //インデックスの番数 53 count = i; 54 break; 55 } 56} 57 58 return count; 59} 60 61//マップAの緯度経度とマップBの緯度経度を格納する変数 62var centerA_pass; 63var centerB_pass; 64 65//initmap関数の変数 66var map; 67var marker = []; 68var infoWindow = []; 69var markerData = []; 70 71//ロード処理 72window.onload = function(){ 73 markerData = markdata_pass(centerA_pass,centerB_pass); 74 initMap(markerData); 75 76} 77 78//markdataの変数にAとBの二拠点を渡す関数 79function markdata_pass(centerA_pass,centerB_pass){ 80 centerA_pass = centerA; 81 centerB_pass = centerB; 82 return Array(centerA_pass, centerB_pass); 83 84 } 85 86 87

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

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

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

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

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

guest

回答1

0

ベストアンサー

First.htmlの中から未ログイン状態で main.js をブラウザがリクエストするため、 未認証状態で最後にリクエストしたURL としてSpringSecurity側は記憶します。
おそらくブラウザの開発者モード(F12)で開いておくと、/js/main.jsへのリクエストが飛んでいるのを確認できるでしょう。

そのためログイン完了後はデフォルトの動作として、未認証状態で最後にリクエストしたURLを復元する動作を行いますので、/js/main.js へ遷移しますので、もしそのJavaScriptが未認証状態でも参照できて問題ないのであれば、SpringSecurityの設定に、JavaScriptも認証状態問わずアクセス可能にすると良いでしょう。

java

1http 2.authorizeRequests() 3.antMatchers("/First.html").permitAll() 4.antMatchers("/failure.html").permitAll() 5.antMatchers("/js/**").permitAll() 6.antMatchers("/templates/**").hasAnyAuthority("ROLE_ADMIN") 7.anyRequest().authenticated() 8(省略)

ないしは、defaultSuccessUrlを使わずに、successFowrardUrlを使い、ログイン後に直接SpringMVCのコントローラへ遷移して、ログイン後続処理を行うのもよく使われます。

投稿2017/06/12 15:31

A-pZ

総合スコア12011

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

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

heavyuseman

2017/06/14 23:30

ご回答ありがとうございます。. antMatchers("/js/**").permitAll()を追記したところ、正常にsucess.htmlに画面遷移しました。 お忙しい中ご回答ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問