【发布时间】:2016-10-27 09:11:32
【问题描述】:
我正在尝试向 angularjs 应用程序添加弹簧安全性。我正在关注本教程关于使用 Spring Security 保护单页应用程序:
https://spring.io/blog/2015/01/12/the-login-page-angular-js-and-spring-security-part-ii
不同之处在于我没有使用 spring boot 而是 spring mvc 用于此目的。我想我添加了我需要的所有东西,但由于某种原因,在输入 inMemory 凭据后,我得到了 403 禁止。
这是我的 spring 安全配置:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
System.out.println("initTT");
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.httpBasic()
.and()
.authorizeRequests()
.antMatchers("/user").hasRole("USER")
.anyRequest().authenticated()
.and()
.csrf().disable();
}
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/app/**");
}
private CsrfTokenRepository csrfTokenRepository() {
HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
repository.setHeaderName("X-XSRF-TOKEN");
return repository;
}
}
我使用了教程中的 authencate() 函数并将其添加到我的 app.js 文件中:
coursesApp.controller('loginController', function($rootScope, $scope, $http, $location) {
var authenticate = function(credentials, callback) {
var headers = credentials ? {authorization : "Basic "
+ btoa(credentials.username + ":" + credentials.password)
} : {};
console.log(headers);
$http.get('/basic-web-app/user', {headers : headers}).success(function(data) {
if (data.name) {
$rootScope.authenticated = true;
} else {
$rootScope.authenticated = false;
}
callback && callback();
}).error(function() {
$rootScope.authenticated = false;
callback && callback();
});
}
authenticate();
$scope.credentials = {};
$scope.login = function() {
console.log("login clicked!!!!!!!");
authenticate($scope.credentials, function() {
if ($rootScope.authenticated) {
console.log("authenticated");
$location.path("/");
$scope.error = false;
} else {
console.log("not authenticated");
$location.path("/login");
$scope.error = true;
}
});
};
});
我有 UserController 和 /用户 教程中描述的端点。我正在用这个扫描包裹
<context:component-scan base-package="com.courses.portal.controllers"/>
我还附上了 chrome 控制台的屏幕截图,以便清楚我在做什么:
web.xml 文件:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/business-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<context:component-scan base-package="com.courses.portal.controllers"/>
<mvc:resources mapping="/app/**" location="/app/build/"/>
<mvc:annotation-driven/>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<security:global-method-security pre-post-annotations="enabled">
<security:protect-pointcut expression="execution(* com.courses.portal.controllers.*.*(..))"
access="ROLE_USER"/>
</security:global-method-security>
</beans>
对不起,如果这是一个常见问题,但我在发布之前尝试在网络上找到任何有用的东西。谢谢!
【问题讨论】:
-
看起来您正在发送正确的标头。您可以从服务器获取该请求的一些 DEBUG 日志吗?
-
我从 mvc-dispatcher-servlet.xml 中删除了一些配置,现在它在控制台中打印出原理:org.springframework.security.authentication.UsernamePasswordAuthenticationToken@4428690f:但是 chrome 开发者控制台说404 未找到:localhost:8087/basic-web-app/user 使用 web.xml 和 mvc-dispatcher-servlet.xml 文件更新了问题
-
我肯定搞砸了配置。当我在 Web 浏览器中粘贴 localhost:8087/basic-web-app/user 时,它返回 HTTP 状态 404 - /basic-web-app/WEB-INF/jsp/user.jsp
-
我认为问题出在视图解析器设置中,因为它正在尝试访问 /basic-web-app/WEB-INF/jsp/user.jsp
-
也许你可以把你的整个项目放在 github 并在这里粘贴一个链接?
标签: java angularjs spring-mvc spring-security