いつもお世話になっております。
Springセキュリティにあるログイン機能を使用した際の動きについて
分からない事があります。
ご存知の方がいらっしゃれば教えて頂けますでしょうか。
【ご質問】
以下のようなconfig、ログイン画面、menu画面、コントローラーがあります。
この状態で、ログイン画面から担当者IDにadmin、パスワードにadminを入力して
ログインボタンを押すと、何故か再度ログイン画面が表示されます。
ログインボタンを押すと、menu画面に遷移させたいのですが、
どのようにすればmenu画面に遷移できるのでしょうか。
(補足)
1.コントローラーのreturn "login/login";、return "login/menu";にbreakpointをはり、
ログインボタンを押した際にどちらに来るかを確認したところ、
return "login/login";に来ました。
2.configのweb.ignoring().antMatchers()に/**を追加して、
ログイン画面から担当者IDにadmin、パスワードにadminを入力して
ログインボタンを押すとmenu画面に遷移できました。
config
@Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers("/static/**","/fonts/**","/jasperreports/**"); } @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/trans/**").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .usernameParameter("tantousya_id") .passwordParameter("password") .permitAll(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("test1").password("test1").roles("USER") .and() .withUser("admin").password("admin").roles("USER","ADMIN"); } }
ログイン画面
<form id="loginForm" method="POST" action="/login/login" > <div class="form-group"> <label for="uname1">担当者ID</label> <input type="text" id="tantousya_id" name="tantousya_id" class="form-control form-control-lg rounded-0" required="required" > </div> <div class="form-group"> <label for="uname1">パスワード</label> <input type="password" id="password" name="password" class="form-control form-control-lg rounded-0" required="required" > </div> <button class="btn btn-success btn-lg float-right" id="btnLogin" type="submit">ログイン</button> </form>
コントローラー
@Controller @Scope("request") @RequestMapping(value="/login") public class RegistContainerLogic { @RequestMapping(method=RequestMethod.GET) public String index(Model model){ return "login/login"; } @RequestMapping(value="/login", method=RequestMethod.POST) public String login(@ModelAttribute("formModel") RegistForm input,Model model){ model.addAttribute("tantousya_id", input.getTantousya_id()); return "login/menu"; } }
menu画面
<!DOCTYPE html> <html lang="ja" xmlns="http://www.thymleaf.org"> <head> <meta http-equiv="Content-type" content="text/html; charset=UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>menu</title> <link rel="stylesheet" th:href="@{/webjars/bootstrap/4.0.0-1/css/bootstrap.min.css}" /> </head> <body> <td th:text="${tantousya_id}"></td> <script type="text/javascript" th:src="@{/webjars/jquery/3.3.1/jquery.min.js}"></script> <script type="text/javascript" th:src="@{/webjars/bootstrap/4.0.0-1/js/bootstrap.min.js}"></script> </body> </html>
RegistForm
public class RegistForm implements Serializable { private static final long serialVersionUID = 1L; private String tantousya_id; private String password; public String getTantousya_id() { return tantousya_id; } public void setTantousya_id(String tantousya_id) { this.tantousya_id = tantousya_id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
以上です。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。