如何从 Spring Security 管理中排除特定资源并使未登录的用户也可以访问它?
Posted
技术标签:
【中文标题】如何从 Spring Security 管理中排除特定资源并使未登录的用户也可以访问它?【英文标题】:How can I exclude a specific resource from Spring Security management and make it accessible also to the not logged users? 【发布时间】:2016-01-17 22:08:52 【问题描述】:我是 Spring Security 的新手,遇到以下问题。
我有这个控制器方法来处理对 /riepilogoCentrale 资源的请求
@RequestMapping(value = "/riepilogoCentrale", method = RequestMethod.GET)
public String riepilogoUtenteCentrale(HttpServletRequest request, Model model, Locale locale)
System.out.println("INTO riepilogoUtenteCentrale()");
return "centrale/riepilogoCentrale";
我的问题是这个资源(因此相关的渲染页面)必须可供所有人(也是未登录的用户)访问,并且如果我尝试以访问者身份访问此资源(未登录),它实际上已配置为 Spring Security用户)Spring 将我重定向到登录页面。
这是我的 Spring Security 配置文件(名为 spring-security.xml):
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-4.0.xsd">
<http pattern="/resources/**" security="none"/>
<http auto-config="true" use-expressions="true" authentication-manager-ref="authenticationManager">
<intercept-url pattern="/login" access="permitAll" />
<intercept-url pattern="/registrati" access="permitAll" />
<intercept-url pattern="/salvaRegistrazione" access="permitAll" />
<intercept-url pattern="/captcha.html" access="permitAll" />
<intercept-url pattern="/**" access="isAuthenticated()" />
<logout logout-success-url="/login" logout-url="/logout" />
<form-login login-page="/login"
authentication-failure-url="/login?error=true"
default-target-url="/"
username-parameter="nomeUtente"
password-parameter="password"
login-processing-url="/j_spring_security_check"/>
<csrf disabled="true"/>
</http>
<authentication-manager id="authenticationManager" >
<authentication-provider>
<jdbc-user-service data-source-ref="datasource"
users-by-username-query="select des_usr_par, des_psw_par,true from TID001_ANAGPARTECIPA where des_usr_par =?"
authorities-by-username-query="select des_usr_par, prg_par from TID001_ANAGPARTECIPA where des_usr_par = ? "/>
</authentication-provider>
</authentication-manager>
</beans:beans>
那么,我怎样才能从 Spring Security 管理中排除 /riepilogoCentrale 并使未登录的用户也可以访问它?
【问题讨论】:
@Makoto 你是对的(我正在处理这个项目,但我还没有配置它)。如果您将其发布为回复,我将接受它:-) 【参考方案1】:您已经为您的一些资源这样做了;例如:
<intercept-url pattern="/salvaRegistrazione" access="permitAll" />
我想你会添加另一个拦截 URL 值,包括 /riepilogoCentrale
作为模式,并根据用户是否经过身份验证在控制器内部实现其他业务逻辑。
【讨论】:
【参考方案2】:您已经排除了一些资源。
<http pattern="/resources/**" security="none"/>
只需在您的 riepilogoCentrale-resource 中添加相同的条目。
【讨论】:
以上是关于如何从 Spring Security 管理中排除特定资源并使未登录的用户也可以访问它?的主要内容,如果未能解决你的问题,请参考以下文章
Spring mvc / security:从spring security中排除登录页面
如何从 Spring Security 中的默认过滤器堆栈中删除一个过滤器?