Java 使用 Vaadin 配置 Spring 安全性
Java Config for Spring Security with Vaadin
我是这些框架的新手 (Vaadin:7.6.1, Spring Security:4.0.3),如果我想构建 Vaadin 应用程序,我在问自己如何配置授权请求.
我查了几个这样写的例子:
@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();
}
}
因为我要设计登录页面所以我用了Thymeleaf engine。因此我正在使用这个控制器 class:
@Controller
public class LoginController
{
@RequestMapping("/login")
String login(Model model)
{
return "login";
}
}
如果我想在用户未登录时阻止我的应用程序的每个请求,我应该定义哪个 .antMatchers()?我知道我必须为登录页面定义 antMatchers("/resources/**").permitAll() 以获取 css 和图像。但是这些像“/UIDL/**”这样的模式是什么?我需要它们做什么?
Which .antMatchers() should I define if I want to block every request
of my application 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();
}
您实际上不需要任何 antMatcher
,甚至对于登录页面也不需要,因为在 .formLogin()
部分,您已经为该页面包含了 .permitAll()
。
现在对于静态资源(css、js、图像)并考虑到 VAADIN,您可以覆盖另一种方法:
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers("/resources/**", "/VAADIN/**");
}
对于 Spring 引导项目,如果我不允许 web.ignoring().antMatchers(...)
中的 "/vaadinServlet/**"
请求,我也发现了问题。
what are these patterns like "/UIDL/**" and what do I need them for?
当服务器收到请求时,Spring 安全性使用这些模式来确定是否应该允许或拒绝访问该请求。
它们代表应用程序上下文根之后的 URI 部分,例如如果您的上下文根是 /
,那么像 http://server.com/UIDL/hello
这样的请求 Spring 安全将用来确定是否授予访问权限的 URI 部分将是 /UIDL/hello
**
代表任何内容,包括任何子级别,例如对于 /UIDL/**
模式,请求 /UIDL/hello/world/and/any/more/levels
将匹配。
还有一个 *
表示任何内容,但不包括子级别,例如对于 /UIDL/*
模式,请求 /UIDL/hello
将匹配,但不匹配 /UIDL/hello/world
.
至于 VAADIN 视图和用户界面,我不确定是否可以使用 antMatchers
来授予或拒绝访问权限,但是您可以使用 [= 注释配置 class 28=] 然后能够在视图上使用 @PreAuthorize( /* spel expression */)
注释来授予或拒绝访问权限。
更新:回答评论问题:
- 为什么使用忽略资源的configure(WebSecurity web)方法而不是允许访问的configure(HttpSecurity http)方法?有显着差异吗?
区别在于WebSecurity#ignoring()
使请求从Spring安全过滤器链中跳过,这是static资源的推荐方式,除静态资源外,其他任何内容都应在 configure(HttpSecurity http)
.
内处理
- 为什么忽略“/VAADIN/**”路径?
因为该路径用于提供主题、小部件集和自定义项,它们是静态内容,所以该路径用于从 Vaadin jar 动态地提供它,但正如 Vaadin 文档中所建议的那样,在生产环境中应该静态服务,因为它更快。
- 我可以想象“/*”和“/**”的意思,但是"UIDL"和"HEARTBEAT"到底是什么意思呢?为什么他们被允许?
UIDL:
User Interface Definition Language (UIDL) is a language for
serializing user interface contents and changes in responses from web
server to a browser. 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.
定期执行心跳请求以验证服务器和客户端之间的连接是否仍然有效,或者会话是否已过期。
我是这些框架的新手 (Vaadin:7.6.1, Spring Security:4.0.3),如果我想构建 Vaadin 应用程序,我在问自己如何配置授权请求.
我查了几个这样写的例子:
@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();
}
}
因为我要设计登录页面所以我用了Thymeleaf engine。因此我正在使用这个控制器 class:
@Controller
public class LoginController
{
@RequestMapping("/login")
String login(Model model)
{
return "login";
}
}
如果我想在用户未登录时阻止我的应用程序的每个请求,我应该定义哪个 .antMatchers()?我知道我必须为登录页面定义 antMatchers("/resources/**").permitAll() 以获取 css 和图像。但是这些像“/UIDL/**”这样的模式是什么?我需要它们做什么?
Which .antMatchers() should I define if I want to block every request of my application 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();
}
您实际上不需要任何 antMatcher
,甚至对于登录页面也不需要,因为在 .formLogin()
部分,您已经为该页面包含了 .permitAll()
。
现在对于静态资源(css、js、图像)并考虑到 VAADIN,您可以覆盖另一种方法:
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers("/resources/**", "/VAADIN/**");
}
对于 Spring 引导项目,如果我不允许 web.ignoring().antMatchers(...)
中的 "/vaadinServlet/**"
请求,我也发现了问题。
what are these patterns like "/UIDL/**" and what do I need them for?
当服务器收到请求时,Spring 安全性使用这些模式来确定是否应该允许或拒绝访问该请求。
它们代表应用程序上下文根之后的 URI 部分,例如如果您的上下文根是 /
,那么像 http://server.com/UIDL/hello
这样的请求 Spring 安全将用来确定是否授予访问权限的 URI 部分将是 /UIDL/hello
**
代表任何内容,包括任何子级别,例如对于 /UIDL/**
模式,请求 /UIDL/hello/world/and/any/more/levels
将匹配。
还有一个 *
表示任何内容,但不包括子级别,例如对于 /UIDL/*
模式,请求 /UIDL/hello
将匹配,但不匹配 /UIDL/hello/world
.
至于 VAADIN 视图和用户界面,我不确定是否可以使用 antMatchers
来授予或拒绝访问权限,但是您可以使用 [= 注释配置 class 28=] 然后能够在视图上使用 @PreAuthorize( /* spel expression */)
注释来授予或拒绝访问权限。
更新:回答评论问题:
- 为什么使用忽略资源的configure(WebSecurity web)方法而不是允许访问的configure(HttpSecurity http)方法?有显着差异吗?
区别在于WebSecurity#ignoring()
使请求从Spring安全过滤器链中跳过,这是static资源的推荐方式,除静态资源外,其他任何内容都应在 configure(HttpSecurity http)
.
- 为什么忽略“/VAADIN/**”路径?
因为该路径用于提供主题、小部件集和自定义项,它们是静态内容,所以该路径用于从 Vaadin jar 动态地提供它,但正如 Vaadin 文档中所建议的那样,在生产环境中应该静态服务,因为它更快。
- 我可以想象“/*”和“/**”的意思,但是"UIDL"和"HEARTBEAT"到底是什么意思呢?为什么他们被允许?
UIDL:
User Interface Definition Language (UIDL) is a language for serializing user interface contents and changes in responses from web server to a browser. 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.
定期执行心跳请求以验证服务器和客户端之间的连接是否仍然有效,或者会话是否已过期。