Java 使用SpringMVC时如何从授权中排除一个url
我正在使用SpringMVC4.1.6编写一个web应用程序,该应用程序使用Java6.45在Tomcat6.0.43上运行。我使用的是基于表单的身份验证,我希望所有页面都是安全的,除了登录页面和资源文件夹,其中包含图像等。我已经阅读了的答案,但它不适用于我。我在浏览器中收到一条错误消息“HTTP状态403-对请求的资源的访问被拒绝”。我花了几个小时试图解决这个问题,但没有成功。如果有人有任何想法,我们将不胜感激。下面显示的是web.xml的相关部分Java 使用SpringMVC时如何从授权中排除一个url,java,spring-mvc,jakarta-ee,tomcat6,Java,Spring Mvc,Jakarta Ee,Tomcat6,我正在使用SpringMVC4.1.6编写一个web应用程序,该应用程序使用Java6.45在Tomcat6.0.43上运行。我使用的是基于表单的身份验证,我希望所有页面都是安全的,除了登录页面和资源文件夹,其中包含图像等。我已经阅读了的答案,但它不适用于我。我在浏览器中收到一条错误消息“HTTP状态403-对请求的资源的访问被拒绝”。我花了几个小时试图解决这个问题,但没有成功。如果有人有任何想法,我们将不胜感激。下面显示的是web.xml的相关部分 <!-- specify whic
<!-- specify which resources are restricted for users -->
<security-constraint>
<web-resource-collection>
<web-resource-name>User</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint><role-name>user</role-name></auth-constraint>
</security-constraint>
<!-- specify which resources are not restricted -->
<security-constraint>
<web-resource-collection>
<web-resource-name>Login</web-resource-name>
<url-pattern>/login</url-pattern>
<url-pattern>/resources/*</url-pattern>
</web-resource-collection>
<!-- no auth-constraint -->
</security-constraint>
<!-- specify all the roles defined for this web app -->
<security-role><role-name>admin</role-name></security-role>
<security-role><role-name>user</role-name> </security-role>
<!-- specify how the user will be authenticatated -->
<form-login-config>
<auth-method>FORM</auth-method>
<form-login-page>/login</form-login-page>
<form-error-page>/login</form-error-page>
</form-login-config>
使用者
/*
事实证明web.xml中有一个错误,tomcat服务器并没有对此进行抱怨。一旦我修复了错误,http 403错误就消失了。
下面是正确的web.xml
<!-- specify which resources are restricted for users -->
<security-constraint>
<web-resource-collection>
<web-resource-name>protected resources</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint><role-name>user</role-name></auth-constraint>
</security-constraint>
<!-- specify which resources are not restricted -->
<security-constraint>
<web-resource-collection>
<web-resource-name>unrestricted resources</web-resource-name>
<url-pattern>/login</url-pattern>
<url-pattern>/resources/*</url-pattern>
</web-resource-collection>
<!-- no auth-constraint -->
</security-constraint>
<!-- specify all the roles defined for this web app -->
<security-role><role-name>admin</role-name></security-role>
<security-role><role-name>user</role-name> </security-role>
<!-- specify how the user will be authenticatated -->
<login-config>
<auth-method>FORM</auth-method>
<realm-name>default</realm-name>
<form-login-config>
<form-login-page>/login</form-login-page>
<form-error-page>/login</form-error-page>
</form-login-config>
</login-config>
保护资源
/*
用户
无限制资源
/登录
/资源/*
管理
使用者
形式
违约
/登录
/登录
我不熟悉这种类型的安全配置,但请尝试将“无授权约束”这一节放在第一位;通常根据找到的第一个匹配项应用安全规则。