如何在未经身份验证的情况下保护某些 URL 而保留其他 URL?
How to secure some URLS while keeping others without authentication?
我有一个 Spring 基于引导的应用程序。我希望 URL /camunda/app/welcome/default/#!/login
无需任何身份验证即可访问,而 URLs
/camunda/app/welcome/default/#!/welcome
,
/camunda/app/welcome/default/#!/dashboard
,
/camunda/app/tasklist/**
,以及
/camunda/app/admin/**
必须受到保护(即只有经过身份验证的用户才能访问它们)。
为了实现这个,我写了如下配置:
@Configuration
@EnableWebSecurity
public class MyConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.requestMatchers()
.and()
.authorizeRequests()
.antMatchers("/camunda/app/welcome/default/#!/login").permitAll()
.antMatchers("/camunda/app/welcome/default/#!/welcome",
"/camunda/app/welcome/default/#!/dashboard",
"/camunda/app/tasklist/**",
"/camunda/app/admin/**",
"/oauth2/authorization/**",
"/oauth2/code/myredirecturl")
.authenticated()
.and()
.oauth2Login(...)
.logout()
.logoutRequestMatcher(...)
.logoutSuccessHandler(...);
}
}
但是使用此配置,未经身份验证的用户可以访问本应受到保护的 URL(/camunda/app/welcome/default/#!/welcome
、/camunda/app/welcome/default/#!/dashboard
、/camunda/app/tasklist/**
、/camunda/app/admin/**
) .
我的配置有什么问题,我该如何解决?
确保在调用端点时使用 #
的 URL 编码,即 %23
。否则,#
之后的字符将不被考虑。
在没有正确编码的情况下向 /camunda/app/welcome/default/#!/welcome
发出请求将被解释为向 /camunda/app/welcome/default/
发出请求。由于该端点不需要身份验证,因此任何人都可以访问它。
由于除 /camunda/app/welcome/default/#!/login
之外的所有端点都需要身份验证,因此您可以压缩 HttpSecurity
配置。我将在下面使用 lambda 样式配置重写它以使其更具可读性:
http
// no need to add requestMatchers since you aren't changing the default configuration
.authorizeRequests(authz -> authz
.antMatchers("/camunda/app/welcome/default/#!/login").permitAll()
.anyRequest().authenticated() // any request that does not match the above rule ^ will require an authenticated user
)
.oauth2Login(...)
.logout(...)
遗憾的是,那是行不通的,因为实际上只有一个 url:
/camunda/app/welcome/default/
和'#'符号后的部分称为'anchors':
#!/欢迎,
#!/仪表板,
锚点不在后端处理,因为它们指向在客户端加载的 html 文档中的某个位置。
所以光靠Spring是解决不了的,一定有前端逻辑。
还有这两个面具:
/camunda/app/tasklist/,以及
/camunda/app/admin/
可以被 Spring 引导覆盖,因为指向不同的 urls,而不是锚点。
我有一个 Spring 基于引导的应用程序。我希望 URL /camunda/app/welcome/default/#!/login
无需任何身份验证即可访问,而 URLs
/camunda/app/welcome/default/#!/welcome
,/camunda/app/welcome/default/#!/dashboard
,/camunda/app/tasklist/**
,以及/camunda/app/admin/**
必须受到保护(即只有经过身份验证的用户才能访问它们)。
为了实现这个,我写了如下配置:
@Configuration
@EnableWebSecurity
public class MyConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.requestMatchers()
.and()
.authorizeRequests()
.antMatchers("/camunda/app/welcome/default/#!/login").permitAll()
.antMatchers("/camunda/app/welcome/default/#!/welcome",
"/camunda/app/welcome/default/#!/dashboard",
"/camunda/app/tasklist/**",
"/camunda/app/admin/**",
"/oauth2/authorization/**",
"/oauth2/code/myredirecturl")
.authenticated()
.and()
.oauth2Login(...)
.logout()
.logoutRequestMatcher(...)
.logoutSuccessHandler(...);
}
}
但是使用此配置,未经身份验证的用户可以访问本应受到保护的 URL(/camunda/app/welcome/default/#!/welcome
、/camunda/app/welcome/default/#!/dashboard
、/camunda/app/tasklist/**
、/camunda/app/admin/**
) .
我的配置有什么问题,我该如何解决?
确保在调用端点时使用 #
的 URL 编码,即 %23
。否则,#
之后的字符将不被考虑。
在没有正确编码的情况下向 /camunda/app/welcome/default/#!/welcome
发出请求将被解释为向 /camunda/app/welcome/default/
发出请求。由于该端点不需要身份验证,因此任何人都可以访问它。
由于除 /camunda/app/welcome/default/#!/login
之外的所有端点都需要身份验证,因此您可以压缩 HttpSecurity
配置。我将在下面使用 lambda 样式配置重写它以使其更具可读性:
http
// no need to add requestMatchers since you aren't changing the default configuration
.authorizeRequests(authz -> authz
.antMatchers("/camunda/app/welcome/default/#!/login").permitAll()
.anyRequest().authenticated() // any request that does not match the above rule ^ will require an authenticated user
)
.oauth2Login(...)
.logout(...)
遗憾的是,那是行不通的,因为实际上只有一个 url:
/camunda/app/welcome/default/
和'#'符号后的部分称为'anchors':
#!/欢迎, #!/仪表板,
锚点不在后端处理,因为它们指向在客户端加载的 html 文档中的某个位置。
所以光靠Spring是解决不了的,一定有前端逻辑。
还有这两个面具:
/camunda/app/tasklist/,以及 /camunda/app/admin/
可以被 Spring 引导覆盖,因为指向不同的 urls,而不是锚点。