我试图在我的Web应用程序中使用Spring Security 3.0.5.基本上,我想拥有一个以json格式返回数据的Web服务HTTP GET.
我已经实现了一个RESTful服务,它在http://localhost:8080/webapp/json请求url时返回数据.使用以下curl命令可以正常工作
> curl http://localhost:8080/webapp/json
{"key":"values"}
Run Code Online (Sandbox Code Playgroud)
使用spring security添加基本身份验证后,我可以使用以下命令获取数据
> curl http://localhost:8080/webapp/json
<html><head><title>Apache Tomcat/6.0.29 - Error report .....
> curl -u username:password http://localhost:8080/webapp/json
{"key":"values"}
Run Code Online (Sandbox Code Playgroud)
前一个命令返回标准的tomcat错误页面,因为它现在需要用户名和密码.我的问题是,是否有可能以打印出我自己的错误消息的方式处理拒绝访问?即
> curl http://localhost:8080/webapp/json
{"error":"401", "message":"Username and password required"}
Run Code Online (Sandbox Code Playgroud)
这是我的spring安全配置和AccessDeniedHandler.正如您所看到的,我正在尝试添加access-denied-handler哪个只是通过servlet响应打印出一个字符串,但它仍然不会在命令行上打印我自己的消息.
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<global-method-security secured-annotations="enabled"/>
<beans:bean name="access-denied" class="webapp.error.JSONAccessDeniedHandler"></beans:bean>
<http auto-config="true">
<access-denied-handler ref="access-denied"/>
<intercept-url pattern="/json" access="ROLE_USER,ROLE_ADMIN" />
<http-basic />
</http>
<authentication-manager>
<authentication-provider>
<password-encoder …Run Code Online (Sandbox Code Playgroud) 我的意思是需要不同:
针对不同的切入点.
这可能吗?
我的Web应用程序有一堆"普通"资源(html页面等)以及一些REST资源,这些资源是由前面提到的html页面从JavaScript调用的.
如果存在会话超时,则用户将被重定向到登录表单.这对于"普通"资源来说非常好,但对于REST资源则不然.我只需要403响应,以便JavaScript可以接管并要求用户重新进行身份验证.
网上有无数的例子如何配置每一个,但我找不到一个如何组合方法的例子.我的所有API网址都以"/ api /"开头,因此我需要所有这些网址的403以及所有剩余网址的重定向.我该如何设置?