2012-04-27 48 views
0

我使用的春天基本身份验证,在我的安全XML以下设置:的Spring Security 3.0:基本验证提示消失

<http use-expressions="true" create-session="never" > 
     <intercept-url pattern="/**" method="GET" access="isAuthenticated()" /> 
     <intercept-url pattern="/**" method="POST" access="isAuthenticated()" /> 
     <intercept-url pattern="/**" method="PUT" access="isAuthenticated()" /> 
     <intercept-url pattern="/**" method="DELETE" access="isAuthenticated()" /> 
     <http-basic /> 
    </http> 

如果认证失败,浏览器弹出窗口提示窗口重新返回到用户名和密码。 有什么办法让这个提示不会弹出吗?

回答

1

最有可能的用于认证失败的页面也受到保护。您可以手动尝试将失败页面设置为一个不保护像

<access-denied-handler error-page="/login.jsp"/> 

连同

<intercept-url pattern="/*login*" access="hasRole('ROLE_ANONYMOUS')"/> 

<intercept-url pattern='/*login*' filters='none'/> 

,或者您可以使用http的auto-config='true'属性元素,将为您解决这个问题。更多here

+0

由于@enterlezi。我尝试添加filters ='none',并尝试将自动配置添加到http元素,但它没有奏效。 据我所知,该响应标题中的提示符显示为** WWW-Authenticate:Basic realm =“Spring Security Application”**。 有没有办法修改响应的标题? – BlackEagle 2012-04-27 10:13:33

+0

我对头文件没有任何线索,但我认为问题在于身份验证失败url仍然受到保护。尝试在http定义中添加表单登录元素,例如''并替换为你的页面(login.jsp)。由于您指定了use-expressions =“true” – dimcookies 2012-04-27 10:51:15

+0

,所以请确保该页面从使用access =“hasRole('ROLE_ANONYMOUS')的过滤器中排除。问题是我没有开发客户端页面。使用基本身份验证 所以我真的没有任何失败的网址重定向到 你知道是否有一种方法,我可以捕捉处理程序中的失败,并在那里我可以控制响应的标头? – BlackEagle 2012-04-27 11:02:27

0

I对于浏览器中的REST API抛出登录对话框也有同样的问题。当你告诉,当浏览器看到的响应头为

WWW身份验证:基本境界=“春季安全应用

它将与基于基本验证dialog.For REST API登录提示,这是不理想的。这是我如何做it.Define一个自定义的身份验证入口点和动工 设置标题为“FormBased”

response.setHeader(“WWW-Authenticat e“,”FormBased“);

应用context.xml的配置下面

<security:http create-session="never" entry-point-ref="authenticationEntryPoint" authentication-manager-ref="authenticationManager"> 
    <security:custom-filter ref="customRestFilter" position="BASIC_AUTH_FILTER" /> 
    <security:intercept-url pattern="/api/**" access="ROLE_USER" /> 
</security:http> 

<bean id="authenticationEntryPoint" class="com.tito.demo.workflow.security.RestAuthenticationEntryPoint"> 
</bean> 

自定义入口点以下类。

@Component 
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint { 

    private static Logger logger = Logger.getLogger(RestAuthenticationEntryPoint.class); 

    public void commence(HttpServletRequest request, HttpServletResponse response, 
          AuthenticationException authException) throws IOException { 
     logger.debug("<--- Inside authentication entry point --->"); 
     // this is very important for a REST based API login. 
     // WWW-Authenticate header should be set as FormBased , else browser will show login dialog with realm 
     response.setHeader("WWW-Authenticate", "FormBased"); 
     response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); 
    } 


} 

注:我已经使用了春天3.2.5.Release

现在,当其他API从像邮递员RESTClient实现击中时,服务器将返回401未经授权。

0

我面临同样的问题,我所做的是在我的资源服务器中创建一个自定义RequestMatcher。这可防止Outh2发送WWW-Authenticate header

实施例:

@Override 

    public void configure(HttpSecurity http) throws Exception { 
     http.requestMatcher(new OAuthRequestedMatcher()) 
       .authorizeRequests() 
       .antMatchers(HttpMethod.OPTIONS).permitAll() 
        .anyRequest().authenticated(); 
    } 

    private static class OAuthRequestedMatcher implements RequestMatcher { 
     public boolean matches(HttpServletRequest request) { 
      String auth = request.getHeader("Authorization"); 
      boolean haveOauth2Token = (auth != null) && auth.startsWith("Bearer"); 
      boolean haveAccessToken = request.getParameter("access_token")!=null; 
    return haveOauth2Token || haveAccessToken; 
     } 
    } 
相关问题