简体   繁体   English

使用Vaadin进行Spring安全性的Java配置

[英]Java Config for Spring Security with Vaadin

Im new to these frameworks (Vaadin:7.6.1, Spring Security:4.0.3) and I'm asking myself how to configure the authorized requests if I want to build a Vaadin application. 我是这些框架的新手(Vaadin:7.6.1,Spring Security:4.0.3),如果我想构建一个Vaadin应用程序,我问自己如何配置授权请求。

I looked up a few examples where something like this is written: 我查了几个例子,里面写着这样的东西:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{

    [...]

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

Because I want to design the login page I use the Thymeleaf engine . 因为我想设计登录页面,所以我使用的是Thymeleaf引擎 Therefore I'm using this Controller class: 因此我正在使用此Controller类:

@Controller
public class LoginController
{
    @RequestMapping("/login")
    String login(Model model)
    {
        return "login";
    }
}

Which .antMatchers() should I define if I want to block every request of my application if the user isn't logged in? 如果用户未登录,我应该定义哪个.antMatchers()我是否要阻止我的应用程序的每个请求? I know that I have to define antMatchers("/resources/**").permitAll() for the login page to get the css and images. 我知道我必须为登录页面定义antMatchers(“/ resources / **”)。permitAll()以获取css和图像。 But what are these patterns like "/UIDL/**" and what do I need them for? 但这些模式如“/ UIDL / **”是什么?我需要它们是什么?

Which .antMatchers() should I define if I want to block every request of my application if the user isn't logged in? 如果用户未登录,我应该定义哪个.antMatchers()我是否要阻止我的应用程序的每个请求?

If you just want to block every request if the user isn't logged in: 如果您只是想在用户未登录时阻止每个请求:

@Override
protected void configure(HttpSecurity http) throws Exception
{
    http
        .authorizeRequests()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login").permitAll()
            .and()
        .logout().permitAll()
            .and()
        .csrf().disable();
}

You don't really need any antMatcher , not even for the login page, as in the .formLogin() part, you already include .permitAll() for that page. 你不需要任何antMatcher ,甚至不需要登录页面,就像.formLogin()部分一样,你已经为该页面包含了.permitAll()

Now for static resources (css, js, images) and with VAADIN in mind, you can do this overriding another method: 现在对于静态资源(css,js,images)和VAADIN,你可以覆盖另一种方法:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring()
        .antMatchers("/resources/**", "/VAADIN/**");
}

With a Spring Boot project, i also found issues if i didn't allow requests to "/vaadinServlet/**" in the web.ignoring().antMatchers(...) . 有了Spring Boot项目,如果我不允许web.ignoring().antMatchers(...)中的"/vaadinServlet/**"请求,我也会发现问题。

what are these patterns like "/UIDL/**" and what do I need them for? 什么是这些模式,如“/ UIDL / **”,我需要它们是什么?

When the server receives a request, Spring Security uses these patterns to determine if it should allow or deny access to the request. 当服务器收到请求时,Spring Security使用这些模式来确定它是应该允许还是拒绝访问请求。

They represent the part of the URI after the context root of your application, eg in the case of your context root being / , then a request like http://server.com/UIDL/hello the part of the URI that Spring Security will use to determine wether to give acces or not will be /UIDL/hello 它们代表应用程序的上下文根之后的URI的一部分,例如,在您的上下文根是/的情况下,然后像http://server.com/UIDL/hello这样的请求是Spring Security将要执行的URI的一部分用于确定是否提供访问将是/UIDL/hello

The ** represents anything including any sub level, eg for the /UIDL/** pattern, the request /UIDL/hello/world/and/any/more/levels will match. **表示包括任何子级别在内的任何内容,例如对于/UIDL/**模式,请求/UIDL/hello/world/and/any/more/levels将匹配。

There's also the single * which represents, anything but not including the sub levels, eg for the /UIDL/* pattern, the request /UIDL/hello will match, but not /UIDL/hello/world . 还有单个*代表,任何但不包括子级别,例如对于/UIDL/*模式,请求/UIDL/hello将匹配,但不是/UIDL/hello/world

As for VAADIN views and UIs, i'm not sure that it is possible to use the antMatchers to grant or deny access, but instead you can annotate the configuration class with @EnableGlobalMethodSecurity(prePost = enabled) and then be able to use the @PreAuthorize( /* spel expression */) annotation on the views to grant or deny access. 至于VAADIN视图和UI,我不确定是否可以使用antMatchers来授予或拒绝访问,而是可以使用@EnableGlobalMethodSecurity(prePost = enabled)注释配置类,然后能够使用@PreAuthorize( /* spel expression */)对视图进行@PreAuthorize( /* spel expression */)注释以授予或拒绝访问权限。

UPDATE : Answering to comment questions: 更新 :回答评论问题:

  1. Why do you use the configure(WebSecurity web) method with ignoring the resources instead of the configure(HttpSecurity http) with allowing access? 为什么使用configure(WebSecurity web)方法忽略资源而不是configure(HttpSecurity http)并允许访问? Are there significant differences? 有显着差异吗?

The difference is that WebSecurity#ignoring() makes the request being skipped from the Spring Security filter chain, and it is the recommended way for static resources, anything else than static resources should be processed inside configure(HttpSecurity http) . 不同之处在于WebSecurity#ignoring()使得从Spring Security过滤器链中跳过请求,并且它是静态资源的推荐方式,除了静态资源之外的任何其他内容都应该在configure(HttpSecurity http)

source 资源

  1. Why do you ignore the "/VAADIN/**" path? 为什么忽略“/ VAADIN / **”路径?

Because that path is used to serve themes, widget sets, and customizations, which is static content, the path is used to serve it dinamycally from the Vaadin jar, but as suggested in the Vaadin documentation, in production environments should be served statically, as it is faster. 由于该路径用于提供主题,窗口小部件集和自定义(静态内容),因此该路径用于从Vaadin jar中以dinamycally方式提供它,但正如Vaadin文档中所建议的那样,生产环境应该静态地提供,如它更快。

source 资源

  1. I could imagine the meaning of "/*" and "/**", but what does "UIDL" and "HEARTBEAT" actually mean? 我可以想象“/ *”和“/ **”的含义,但“UIDL”和“HEARTBEAT”究竟是什么意思? Why are they permitted? 他们为什么被允许?

UIDL: UIDL:

User Interface Definition Language (UIDL) is a language for serializing user interface contents and changes in responses from web server to a browser. 用户界面定义语言(UIDL)是一种用于序列化用户界面内容以及从Web服务器到浏览器的响应更改的语言。 The idea is that the server-side components "paint" themselves to the screen (a web page) with the language. 这个想法是服务器端组件使用该语言将自己“绘制”到屏幕(网页)。 The UIDL messages are parsed in the browser and translated to GWT widgets. UIDL消息在浏览器中解析并转换为GWT小部件。

source 资源

Heartbeat requests are performed periodically to verify that the connection is still alive between server and client, or the session haven't expired. 定期执行心跳请求以验证服务器和客户端之间的连接是否仍处于活动状态,或者会话是否已过期。

source - see sections 4.8.5, 4.8.6, 4.8.7 and 4.8.8 来源 - 见4.8.5,4.8.6,4.8.7和4.8.8节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM