大家好,我将在我的 web 应用程序中集成我的 restful web 服务的身份验证。我已经为我的整个应用程序集成了 Spring security 3.1 并且它运行良好;但我很困惑,我真的卡住了,如何在现有应用程序中集成 Web 服务的安全性?这是我现有的身份验证和授权安全配置。
安全应用上下文.xml :
<beans:bean id="myAccessDecisionManager"
class="com.security.repository.MyAccessDecisionManager">
<beans:property name="customAuthenticatiuonService" ref="customAuthenticatiuonServiceImpl"> </beans:property>
</beans:bean>
<http auto-config="true" once-per-request="true"
access-decision-manager-ref="myAccessDecisionManager" access-denied-page="/jsp/errorPage.jsp">
<intercept-url pattern="/*.web" access="ROLE_ANONYMOUS" />
<intercept-url pattern="productsService/*.web" access="ROLE_ADMIN" />
<!-- Override default login and logout pages -->
<form-login login-page="/login.works" login-processing-url="/j_spring_security_check"
default-target-url="/login/validate.works"
authentication-failure-url="/login.works?login_error=1" />
<logout logout-url="/j_spring_security_logout"
logout-success-url="/login.works" invalidate-session="true" />
<session-management invalid-session-url="/login.works"
session-fixation-protection="newSession">
<concurrency-control max-sessions="100"
error-if-maximum-exceeded="false" />
</session-management>
</http>
<authentication-manager>
<authentication-provider ref="customAuthenticationProvider"></authentication-provider>
</authentication-manager>
<beans:bean id="customAuthenticationProvider"
class="com.security.repository.CustomAuthenticationProvider">
<beans:property name="customAuthenticatiuonService" ref="customAuthenticatiuonServiceImpl"> </beans:property>
</beans:bean>
<beans:bean id="customAuthenticatiuonServiceImpl"
class="com.service.impl.CustomAuthenticatiuonServiceImpl">
<beans:property name="customAuthenticationDAO" ref="customAuthenticationDAOTarget"> </beans:property>
</beans:bean>
<beans:bean id="customAuthenticationDAOTarget" class="com.dao.impl.CustomAuthenticatiuonDAOImpl">
<beans:property name="hibernateTemplate" ref="cessHibernateTemplate"/>
</beans:bean>
现在我希望保护我的网络服务,如下所示:
我的网络服务:
@Component
@Path("/productsService")
//@RequestMapping("/productsService")
@Scope("request")
@Controller
public class ProductsController {
@Autowired
private ProductsService products;
@GET
@Path("/getProductsList.lbt")
// @RequestMapping("/getProductsList.lbt")
@Produces("text/plain")
public String getProductsList() {
return products.getProductsList();
}
}
@Service("products")
public class ProductsService {
@Secured("ROLE_ADMIN")
public String getProductsList() {
return "Test String for Rest Web Service";
}
}
最后我的客户课程:
public static void main(String[] args) {
Client c = Client.create();
// plain text
WebResource r = c
.resource("http://localhost:8080/productsService/getProductsList.web");
c.addFilter(new HTTPBasicAuthFilter("admin", "admin"));
c.setFollowRedirects(false);
System.out.println("Plain Text=>> " + r.get(String.class));
}
我正在使用 customAuthenticationManager 和 myAccessDecisionManger 对用户进行身份验证和授权。当我在控制器上使用 @Path 注释并对其进行调试时,调试器无法转到我的控制器并抛出 402 未找到的错误,当我使用 @RequestMapping 时它会正常运行,但是当从控制器返回时,我在客户端收到错误 302成立。如何解决这个问题?请帮我。提前致谢。