Hi, Mình đang tìm hiểu Spring Security với JWT có thắc mắc làm sao Spring có thể so sánh password nhập vào là đúng. Tức là luồng data nó sẽ gửi tới server như thế nào ? (đang vướng mắc vấn đề này mà chưa giải đáp được)
Nhiệm vụ của
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
là để làm gì vậy ?
UserDetails userDetails = userDetailsService.loadUserByUsername(JsonUtil.convertToJson(user));
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(
userDetails,
userDetails.getUsername(),
userDetails.getAuthorities());
auth.setDetails(userDetails);
SecurityContext securityContext = SecurityContextHolder.getContext();
securityContext.setAuthentication(auth);
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
SecurityConfig :
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Autowired
public void globalUserDetails(final AuthenticationManagerBuilder auth) throws Exception {
System.out.println("globalUserDetails ...");
auth.userDetailsService(userDetailsService);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
/*http.authorizeRequests()
.antMatchers("/resources/**").permitAll()
.antMatchers("/login**").permitAll()
.antMatchers("/api/mobile/v1/login").permitAll()
.anyRequest().authenticated()
.and().formLogin().loginPage("/login").permitAll()
.failureUrl("/login-error").usernameParameter("username").passwordParameter("password").and().logout()
.logoutSuccessUrl("/login").permitAll().and().rememberMe()
.key(applicationSettings.getRemmember_secret()).rememberMeServices(rememberMeServices()).and().csrf()
.disable();*/
http
.httpBasic().disable()
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/resources/**").permitAll()
.antMatchers("/login**").permitAll()
.antMatchers("/api/mobile/v1/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").permitAll()
.failureUrl("/login-error").usernameParameter("username").passwordParameter("password")
.and()
.logout().logoutSuccessUrl("/login").permitAll()
.and()
.rememberMe()
.key(applicationSettings.getRemmember_secret()).rememberMeServices(rememberMeServices())
.and()
.apply(new JwtSecurityConfigurer(jwtTokenProvider));
}
Controlller :
@PostMapping(path = "/login")
public String checkLogin(
@RequestParam("branchCode") String branchCode,
@RequestParam(value = "username") String username,
@RequestParam(value = "password") String password) {
System.out.println("CheckLogin in this request !");
Screenright sc = screenrightService.findByEmployeeAndBranch(username, password, branchCode);
Map<String, String> result = new HashMap<String, String>();
if (sc == null) {
result.put("branchCode", "");
result.put("username", "");
result.put("password", "");
result.put("error", "false");
return JsonUtil.convertToJson(result);
}
List<String> user = new ArrayList<>();
user.add(username);
user.add(branchCode);
// System.out.println("AuthenticationManager !");
System.out.println("Load User By Username !");
UserDetails userDetails = userDetailsService.loadUserByUsername(JsonUtil.convertToJson(user));
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(
userDetails,
userDetails.getUsername(),
userDetails.getAuthorities());
auth.setDetails(userDetails);
SecurityContext securityContext = SecurityContextHolder.getContext();
securityContext.setAuthentication(auth);
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
result.put("branchCode", branchCode);
result.put("username", username);
System.out.println("Create token from login !");
result.put("token", jwtTokenProvider.createToken(username, branchCode));
return JsonUtil.convertToJson(result);
}