我有一个单独的正面和背面的Web应用程序:
/project
+--- /back
+--- /front
后面是使用Spring启动Spring MVC开发的,而前端是使用AngularJS .
我正在尝试为后面/前面之间的通信设置安全性 . 我做了什么:
- create a ConfigSecurity class which extends from WebSecurityConfigurerAdapter
- create a SpringWebMvcInitializer which extends from AbstractAnnotationConfigDispatcherServletInitializer and call ConfigSecurity
- create a SecurityWebInitializer class which extends AbstractSecurityWebApplicationInitializer
我的ConfigSecurity看起来像这样:
@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ConfigSecurity extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/demo/**","/home").permitAll()
.anyRequest().fullyAuthenticated()
.and()
.formLogin().loginPage("...")
.and()
.httpBasic();
}
}
我的问题:
-
在
configureGlobal()
中,我可以设置用户名和密码来访问我的受保护网址,但是如果不只有一个用户,我该怎么办?我的意思是,我想授予对我的数据库中注册的所有用户的访问权限 . -
由于后面和前面只与REST通信(通过JSON文件),
ConfigSecurity
中的_1004685应该怎么做?默认情况下,Spring Security会生成默认登录表单 . 我不需要在应用程序的后面,因为它是负责显示loginPage的前端 . 如何跳过后面的登录页面?也许通过将用户名和密码放在JSON文件中,前端发送到后面?有人知道怎么做吗?
我正在使用Java配置Spring(不是XML配置) .
2 回答
你有很多选择,所有这些选项都需要相当多的代码,但大部分代码已经在互联网上很好地实现了,所以你只需要让它满足你的需求 . 您可以采用最简单的方式,即http会话,JSON令牌或JWT(JSON Web令牌)或其他任何方式 .
Spring博客有一篇很长的帖子,关于如何在这里实现不同的解决方案:https://spring.io/guides/tutorials/spring-security-and-angular-js/
JHipster还有不同的身份验证方法,您可以查看 . 他们已经很好地实现了前端的角色安全性,我非常喜欢并且真的建议调查:https://github.com/jhipster/jhipster-sample-app
这里也是一个需要最多代码的JWT:http://blog.jdriven.com/2014/10/stateless-spring-security-part-2-stateless-authentication/
HTTP会话绝对是最容易设置的,并且已经得到了Spring Security的良好支持 .
在baeldung website(即使它使用的是XML配置)和this website(使用Java配置)上找到了很多帮助 .
有关Spring安全性如何工作的全局解释,this link对我帮助很大 .