Итак, у меня есть этот проект, и я действительно хочу, чтобы пользователь мог войти в систему и получить доступ к определенной странице:
Безопасность
@Configuration
@EnableWebSecurity
public class MainSecurityConfig extends WebSecurityConfigurerAdapter {
@Resource
private UserDetailsServiceImpl userDetailsService;
@Bean
public HttpSessionEventPublisher httpSessionEventPublisher() {
return new HttpSessionEventPublisher();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable();
http
.authorizeRequests()
.antMatchers("/", "/**", "/login/**", "/index.html", "/login.html", "/components/**", "/css/**", "/js/**", "/fonts/**", "/images/**", "/.sass-cache/**", "/services.html").permitAll()
.anyRequest().authenticated();
http.formLogin()
.loginPage("/login")
.failureForwardUrl("/login.html")
.usernameParameter("user")
.passwordParameter("password");
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider());
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
authProvider.setUserDetailsService(userDetailsService);
authProvider.setPasswordEncoder(passwordEncoder());
return authProvider;
}
Когда я отправлял запрос / login от angularjs как POST, я нажимал на UserDetailsServiceImpl
, что хорошо, но имя пользователя было пустым.
UserDetailsServiceImpl
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
@Resource
private HttpSession httpSession;
@Resource
private UserDao userDao;
@Override
public UserDetails loadUserByUsername(String user) throws UsernameNotFoundException {
User userByEmail = userDao.findUserByEmail(user);
UserDetailsImpl userDetails = new UserDetailsImpl(userByEmail, httpSession.getId());
return userDetails;
}
}
Итак, я немного погуглил, и он сказал, что запрос / login должен быть GET
, что само по себе меня смутило, должны ли мы действительно вводить имя пользователя и пароль в URL?Или я думаю об этом неправильно.В любом случае, вот код angularJS:
$scope.loginUser = function () {
$scope.user.user = $scope.email;
$scope.user.password = $scope.password;
$http.get("/login", { params: {username: $scope.user.user, password: $scope.user.password}});
Я больше не бью точки останова в пределах UserDetailsServiceImpl
, а скорее получаю 404.
ОБНОВЛЕНИЕ
После обновления URL-адреса обработки я теперь публикую его, но имя пользователя, которое передается на стороне сервера, пусто
$scope.loginUser = function () {
$scope.user.username = $scope.email;
$scope.user.password = $scope.password;
$http.post("/api/authentication", $scope.user);
Все до этого в порядке, просто когда Java обрабатывает его