Я реализовал приложение весенней загрузки, чтобы использовать провайдер аутентификации.Функциональность корректно работает, когда я использую httpbasic режим аутентификации.Однако, когда я использую пользовательскую страницу входа, страница входа отображается перед доступом к любой странице.Но после входа он остается на той же странице.Операторы печати, которые я поместил в провайдера нестандартной аутентификации, вызываются при использовании режима аутентификации httpbasic, но не при использовании пользовательской страницы входа.Ниже приведен код
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
private CustomAuthenticationProvider aCustomAuthenticationProvider;
@Autowired
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(aCustomAuthenticationProvider);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/mybank/**").permitAll()
.anyRequest().authenticated()
.and()
.httpBasic()
//.formLogin()
//.loginPage("/login")
//.defaultSuccessUrl("/loginUser")
.and()
.logout()
.permitAll();
}
}
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider{
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String uname = authentication.getName();
String password = authentication.getCredentials().toString();
System.out.println("User Name: "+uname);
System.out.println("Password: "+password);
List<GrantedAuthority> grantedAuths = new ArrayList<>();
grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
grantedAuths.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
return new UsernamePasswordAuthenticationToken(authentication.getName(), authentication.getCredentials(), grantedAuths);
}
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
@RequestMapping("/login")
public ModelAndView loginPage()
{
Customer cust = new Customer();
ModelAndView mv = new ModelAndView();
mv.addObject("customer", cust);
mv.setViewName("login");
return mv;
}
@RequestMapping("/loginUser")
public String login(@ModelAttribute("customer")Customer cust){
return "homepage";
}
My login.jsp is as below
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head></head>
<body>
<h3>Welcome, Please Login</h3>
<form:form method="POST" action="/mybank/loginUser"
modelAttribute="customer">
<table>
<tr>
<td><form:label path="custId">Customer ID</form:label></td>
<td><form:input path="custId" /></td>
</tr>
<tr>
<td><form:label path="password">Password</form:label></td>
<td><form:password path="password" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
</body>
</html>