我想知道最好的方法是设置一个包含Spring RESTful API的项目,以及提供静态Angularjs页面以使用RESTful Web服务的能力。以下实现有效但我现在希望在应用程序中添加安全性,我不确定如何将Spring Security应用于REST Api和静态页面。
我有以下项目结构
Servlet配置
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
@ComponentScan
public class WebAppInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.addFilter("corsFilter", new CORSFilter());
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(WebMvcConfig.class);
ctx.setServletContext(servletContext);
Dynamic dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
dynamic.addMapping("/api/*");
dynamic.setLoadOnStartup(1);
}
}
答案 0 :(得分:1)
我会按照spring.io博客上的一系列文章来解释你正在寻找什么:Spring + Security + Angular JS。
这里的文章:
http://spring.io/blog/2015/01/12/spring-and-angular-js-a-secure-single-page-application https://spring.io/blog/2015/01/12/the-login-page-angular-js-and-spring-security-part-ii http://spring.io/blog/2015/01/20/the-resource-server-angular-js-and-spring-security-part-iii http://spring.io/blog/2015/01/28/the-api-gateway-pattern-angular-js-and-spring-security-part-iv http://spring.io/blog/2015/02/03/sso-with-oauth2-angular-js-and-spring-security-part-v