• 1000 войдите снова (или с любым другим пользователем) появляется ошибка: Произошла непредвиденная ошибка (тип = Не найдено, статус = 404). Сообщение недоступно
Контроллер
@RequestMapping(value = "/loginIndex", method = RequestMethod.GET)
public String loginIndex(Model model, String error, String logout) {
if (error != null)
model.addAttribute("error", "Your username and password is invalid.");
if (logout != null)
model.addAttribute("message", "You have been logged out successfully.");
return "loginIndex";
}
@RequestMapping(value = {"/welcome"}, method = RequestMethod.GET)
public String welcome(Model model) {
return "welcome";
}
@RequestMapping(value = {"/","/index"}, method = RequestMethod.GET)
public String index(Model model) {
return "index";
}
LoginIndex. jsp
<form method="POST" action="${contextPath}/loginIndex" class="form-signin">
<input name="username" type="text" class="border p-3 w-100 my-2 form-control" placeholder="Username"/>
<input name="password" type="password" class="border p-3 w-100 my-2 form-control" placeholder="Password"/>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form>
SecurityConfig
@Configuration
@Order(1)
public static class WebSecurityConfig1 extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/resources/**", "/index","/loginIndex","/registration").permitAll()
.antMatchers("/user/**").access("hasRole('USER')")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/index")
.failureUrl("/loginIndex?error")
.usernameParameter("username")
.passwordParameter("password")
.defaultSuccessUrl("/welcome")
.loginProcessingUrl("/loginIndex")
.permitAll()
.and()
.logout()
.permitAll();
}
}
WelcomePage. jsp
<c:if test="${pageContext.request.userPrincipal.name != null}">
<form id="logoutForm" method="POST" action="${contextPath}/logout">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form>
<h2>Welcome ${pageContext.request.userPrincipal.name} | <a onclick="document.forms['logoutForm'].submit()">Logout</a></h2>
</c:if>