今、Webアプリケーションを作成中です。そのWebアプリケーションの各ページからログイン画面に遷移できるのですが、ログインに成功したらトップ画面に遷移してしまいます。
どうしたら、ログイン画面に遷移する前の画面にもどることができるのでしょうか?
Spring Securityの設定
Java
1package com.koikeya.project1.app.config; 2 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.context.annotation.Bean; 5import org.springframework.context.annotation.Configuration; 6import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 7import org.springframework.security.config.annotation.web.builders.HttpSecurity; 8import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 9import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 10import org.springframework.security.core.userdetails.UserDetailsService; 11import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 12import org.springframework.security.crypto.password.PasswordEncoder; 13 14/** 15 * Spring Security設定クラス 16 * @author Yuki Koike 17 */ 18@Configuration 19// @EnableWebSecurityを付与して、Spring SecurityのWeb連携機能(CSRF対策など)を有効にする 20@EnableWebSecurity 21public class SecurityConfig extends WebSecurityConfigurerAdapter { 22 23 /** 24 * UserDetailsService 25 */ 26 @Autowired 27 UserDetailsService userDetailsService; 28 29 /** 30 * パスワード符号化用オブジェクトを返す 31 * 32 * @return BCryptPasswordEncoderオブジェクト 33 */ 34 @Bean 35 PasswordEncoder passwordEncoder() { 36 return new BCryptPasswordEncoder(); 37 } 38 39 /* 40 * (非 Javadoc) 41 * @see org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter#configure(org.springframework.security.config.annotation.web.builders.HttpSecurity) 42 */ 43 @Override 44 protected void configure(HttpSecurity httpSecurity) throws Exception { 45 httpSecurity.authorizeRequests().antMatchers("**", "**/**", "/css/**", "/images/**", "/js/**", "/webjars/**") 46 .permitAll().anyRequest().authenticated().and() 47 .formLogin().loginPage("/login") 48 //.defaultSuccessUrl("/index.html") 49 .failureUrl("/login?error=true").permitAll() 50 .and().logout().logoutSuccessUrl("/login").permitAll(); 51 } 52 53 @Override 54 protected void configure(AuthenticationManagerBuilder auth) throws Exception { 55 auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); 56 } 57} 58
ログイン成功の設定
Java
1package com.koikeya.project1.app.controller; 2 3import java.io.IOException; 4 5import javax.servlet.ServletException; 6import javax.servlet.http.HttpServletRequest; 7import javax.servlet.http.HttpServletResponse; 8 9import org.springframework.security.core.Authentication; 10import org.springframework.security.web.DefaultRedirectStrategy; 11import org.springframework.security.web.RedirectStrategy; 12import org.springframework.security.web.authentication.AuthenticationSuccessHandler; 13 14/** 15 * AuthenticationSuccessHandler実装クラス 16 */ 17public class AuthenticationSucessHandlerImpl implements AuthenticationSuccessHandler { 18 19 /* 20 * (非 Javadoc) 21 * @see org.springframework.security.web.authentication.AuthenticationSuccessHandler#onAuthenticationSuccess(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.springframework.security.core.Authentication) 22 */ 23 @Override 24 public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, 25 Authentication authentication) throws IOException, ServletException { 26 27 RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 28 redirectStrategy.sendRedirect(request, response, "/index.html"); 29 } 30} 31
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。