[英]Spring Boot Security /login authentication with angularJS
因此,我有了这个项目,实际上我要做的就是能够让用户登录并访问特定页面:
安全
@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;
}
当我从angularjs发送POST的/ login请求时,我碰到了很好的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。
UPDATE
更新处理网址后,我现在将其发布,但是通过服务器端传递的用户名为空
$scope.loginUser = function () {
$scope.user.username = $scope.email;
$scope.user.password = $scope.password;
$http.post("/api/authentication", $scope.user);
到这里为止的一切都很好,只是在Java处理它的时候
如果您使用的是Angular,则由于您正在编写SPA且没有由Angular本身管理页面导航的loginPage
页面。
您应该使用loginProcessingUrl
,它仅定义登录提交网址
.and()
.formLogin()
.loginProcessingUrl("/api/authentication")
.usernameParameter("username")
.passwordParameter("password")
要提交您的登录名,您需要执行POST
而不是GET
。 链接可能意味着访问登录页面而不提交的URL。
在上面的示例中,您必须使用url /api/authentication
和包含username
和password
的正文进行POST
另外,如果我已经看到您已经找到了解决方案,那么我已经发布了一个基于Spring Boot 2.0和angular 6(Angularjs已经过时)的项目,该项目具有spring安全性和有状态身份验证(与您搜索的相同)
https://github.com/ValerioMC/vforge-stateful-auth
这只是一个起点。
问题出在我的AngularJS $http.post
请求中,我通过为“ application / x-www-form-urlendcoded”添加标头解决了它,因为它不是默认标头:
$http({
method: 'POST',
url: '/api/authentication',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: 'username=' + $scope.email + '&password=' + $scope.password
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.