如何使用Spring Security处理Vaadin中的拒绝访问

时间:2014-03-18 10:01:26

标签: spring-security vaadin vaadin7

我在使用Vaadin和Spring Security(通过AD使用auth)一起工作时遇到了严重问题。我设法让认证位正常工作"足够好" (用我自己的处理程序),但是当我试图让授权位工作时 - 我遇到了更多麻烦。让我们分解并采取我们应该处理拒绝访问的方式。

所以,当然我尝试将它设置为&#34;首选&#34;通过简单地使用弹簧&#34; auto&#34;访问被拒绝处理程序,如下所示:<access-denied-handler error-page="/403"/>,但是会从Vaadin中弹出一条消息说:

&#34;无法加载引导程序javascript:./ VAADIN / evadinBootstrap.js&#34;

我还尝试了自己的自定义访问拒绝处理程序。被覆盖的handle方法被调用不少于20次,然后浏览器(在这种特殊情况下为FF)给我这样的信息:

&#34;页面未正确重定向 Firefox检测到服务器正在以永远不会完成的方式重定向此地址的请求。 有时可能会因禁用或拒绝接受Cookie而导致此问题。&#34;

这很可能是因为用户未获得重定向页面的授权,但我将其设置为:<intercept-url pattern="/#!access_denied_view" access="permitAll" />,我还尝试了<intercept-url pattern="/accessdenied.jsp" access="permitAll" />,导致上述&#34; bootstrap&#34; -error。

我做错了什么?为什么在这种情况下甚至会调用Vaadin?

编辑:添加了多次调用handle-method

的可能原因

3 个答案:

答案 0 :(得分:1)

您必须允许Vaadin特定路径并在spring security中禁用csrf。 Vaadin在客户端和服务器之间拥有自己的csrf管理。

例如spring boot 1.3.0.RC1 java配置:

http.authorizeRequests().antMatchers("/VAADIN/**", "/PUSH/**", "/UIDL/**", "/vaadinServlet/UIDL/**").permitAll();
http.csrf().disable();

答案 1 :(得分:0)

亚历山大指出,瓦丁有自己的CSRF features。 Spring Security CSRF是全局的,必须完全关闭所有请求。另一种选择是创建一个小的请求匹配器,为每个请求启用或禁用Spring Security。然后,您仍然可以对Vaadin应用程序之外的所有请求(例如REST或其他页面)运行CSRF支持。

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.util.matcher.RegexRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(final HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .antMatchers("/", "/support").permitAll()
            .anyRequest().fullyAuthenticated()
            .and().formLogin()
            .and().httpBasic()
            .and().csrf().requireCsrfProtectionMatcher(new RequestMatcher() {

                private RegexRequestMatcher nsdoc = 
                        new RegexRequestMatcher("/open/*", null);

                @Override
                public boolean matches(final HttpServletRequest arg0) {

                    if (nsdoc.matches(arg0)) {
                        return true;
                    } else {
                        return false;
                    }
                }
            });

在此配置中,URI //support下的所有请求都是允许的,无需登录。 URI /open下的所有内容都将由Spring CSRF功能处理,而其余部分(如Vaadin)将自行处理。

答案 2 :(得分:0)

您需要允许(添加到antMatchers)所有Vaadin特定的拒绝路径。下面是一个配置方法覆盖示例;我使用Vaadin的Spring身份验证作为/ home下的主要用户界面。

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
                .antMatchers("/resources/**", "/webjars/**", "/VAADIN/**", "/PUSH/**", "/UIDL/**", "/vaadinServlet/UIDL/**","/vaadinServlet/HEARTBEAT/**", "/registration").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    http.csrf().ignoringAntMatchers("/home**", "/VAADIN/**", "/PUSH/**", "/UIDL/**", "/vaadinServlet/UIDL/**","/vaadinServlet/HEARTBEAT/**");
}

因此,在Spring Csrf上需要忽略相同的Vaadin路径列表