reactjs - 如何使用 Spring Security 允许访问特定的 React Hash 路由?

标签 reactjs spring-security react-router jwt spring-security-oauth2

我试图限制未经身份验证的用户访问我们的 Reactjs 单页应用程序中的任何路由。我们使用 HashRouter 来处理 UI 端的路由,因此我们的 url 看起来像 http://localhost/app/#/Home , http://localhost/app/#/Login等等。我们将 Spring Security OAuth2 与 JWT 结合使用。

当向应用程序发出请求时,所有请求都以/app/形式传入,而不是/app/#/Login、/app/#/Home 等。这对我来说很有意义,因为路由是在客户端呈现正确的路线,但这会导致尝试保护应用程序的问题。为了允许访问登录路由,我需要允许 Spring Security 中的所有人访问/app/,这将打开一切。

是否可以在 Spring Security 中保护 React Hash 路由,或者我必须在路由器的 UI 端处理这个问题?

登录配置

@EnableWebSecurity(debug = true)
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE + 1)
public class LoginConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .antMatcher("/app/#/login")
                .httpBasic().disable()
                .formLogin().disable()
                .authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS).permitAll()
                .antMatchers(HttpMethod.GET, "/favicon.ico").permitAll()
                .antMatchers("/app/#/login").permitAll()
                .anyRequest().authenticated()
        ;
    }
}

登录配置授予所有人访问权限

@EnableWebSecurity(debug = true)
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE + 1)
public class LoginConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .antMatcher("/app/**")
                .httpBasic().disable()
                .formLogin().disable()
                .authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS).permitAll()
                .antMatchers(HttpMethod.GET, "/favicon.ico").permitAll()
                .antMatchers("/app/**").permitAll()
                .anyRequest().authenticated()
        ;
    }
}

所有其他请求的默认配置

@EnableWebSecurity(debug = true)
@Configuration
@Order(2)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .httpBasic().disable()
                .formLogin().disable()
                .csrf().disable()
                .authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS).permitAll()
                .antMatchers(HttpMethod.GET, "/**/favicon.ico").permitAll()
                .antMatchers(HttpMethod.POST, "/oa/oauth/token").permitAll()
                .anyRequest().authenticated()
        ;
    }
}

提前致谢!

最佳答案

你不能。构建 SPA 时,您将安全重点转移到保护 SPA 访问的 API 端点,而不是保护“页面”或“SPA 路由”。因此,只需保护向 SPA 提供数据和功能的服务器端点即可。

例如,如果不允许用户访问“管理”功能,则不要尝试阻止路由 /app/#Admin,而是确保并阻止所有与管理相关的 api端点(例如 /api/admin/api/adduser...)。

这并不意味着您不应该在 SPA 中添加代码来隐藏禁止的链接或阻止用户访问该路径 - 您应该这样做,因为它可以带来更好的用户体验。只需要知道,在 SPA 中隐藏路由纯粹是为了用户体验,而不是安全性。安全性是通过保护 API 来处理的。

关于reactjs - 如何使用 Spring Security 允许访问特定的 React Hash 路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46815713/

相关文章:

javascript - React-router v4 this.props.history.push(...) 不起作用

javascript - 在 ReactJs 中,组件和元素有什么区别?

java - Spring 中的华夫饼自定义错误页面

reactjs - NextJS 中路由器和链接的典型区别

Grails 在 SecurityFilters 中使用 Spring Security

java - Spring Security 找不到数据源

typescript - 将 react-router 与 typescript 一起使用

reactjs - Jest 手动模拟在 CRA 中不使用模拟文件

javascript - 自定义迭代器方法在渲染中不起作用

node.js - Webpack 和 Babel : Server Side rendering of React Component "Unexpected token ' <'"