Hej,
chciałbym logować się za pomocą REST API zamiast używając Thymeleafa, ale nie do końca wiem jak to przerobić.
Chiwilowo mam controller:
@Controller
public class LoginController {
@GetMapping("/login")
public String login () {
return "login";
}
}
oraz szablon:
<!DOCTYPE html>
<html xmlns:th="http://thymeleaf.org">
<head>
<title>Login</title>
</head>
<body>
<form action="" method="post">
<fieldset>
<legend>Please Login</legend>
<!-- use param.error assuming FormLoginConfigurer#failureUrl contains the query parameter error -->
<div th:if="${param.error != null}">
Failed to login.
<div th:if="${SPRING_SECURITY_LAST_EXCEPTION != null}">
Reason: <span
th:text="${SPRING_SECURITY_LAST_EXCEPTION.message}"></span>
</div>
</div>
<!-- the configured LogoutConfigurer#logoutSuccessUrl is /login?logout and contains the query param logout -->
<div th:if="${param.logout != null}">You have been logged out.</div>
<p>
<label for="username">Username</label> <input type="text"
id="username" name="username" />
</p>
<p>
<label for="password">Password</label> <input type="password"
id="password" name="password" />
</p>
<div>
<button type="submit" class="btn">Log in</button>
</div>
</fieldset>
</form>
</body>
</html>
SecurityConfig wygląda następująco:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Autowired
private UserDetailsServiceImpl userDetailsService;
@Autowired
private PasswordEncoder passwordEncoder;
@Override
protected void configure(HttpSecurity http) throws Exception
{
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/register")
.permitAll()
.and()
.authorizeRequests()
.antMatchers("/admin/**").hasAnyRole("ADMIN")
.anyRequest().hasAnyRole("USER").and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/dashboard")
.permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception
{
auth
.userDetailsService(userDetailsService)
.passwordEncoder(passwordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
Po stronie klienta chciałbym używać Angulara i logować się poprzez wysyłanie JSON'a na server. Nie do końca wiem jak skonfigurować controller. Mógłby ktoś podpowiedzieć?