【问题标题】:Secure react routing with spring security使用 Spring Security 保护反应路由
【发布时间】:2019-10-13 14:33:27
【问题描述】:

我在我的项目中介绍了通过 spring security 和 jwt auth 登录/注销。目前我想防止未经授权的用户进入像 /admin 这样的路由。我是否应该在拨打 history.push("/admin") 之前拨打电话并检查响应以确定用户是否应该看到它?

如何防止访问路由?我不是说要调用 api...

问候

【问题讨论】:

    标签: spring reactjs spring-security jwt


    【解决方案1】:

    如果您使用的是 redux,则将用户的登录状态存储为 react 状态或全局状态。

    然后你可以在 react 中创建一个私有路由并检查用户角色

    const PrivateRoute = (props)=>(
      <Route {...props.routeProps} render={() => (
        props.logged_in===true ? (
            <div>{props.children}</div>
            ) : (
            <Redirect to={{
                pathname: '/',
                state: { from: props.location }
            }} /> )
        )} />
    )
    

    你可以像这样使用这个组件

    <PrivateRoute path = {"/admin/"} ><Dashboard /></PrivateRoute>
    

    【讨论】:

    • 安全吗?我猜你可以通过 React 开发工具轻松访问状态?
    • 没有代码在客户端是安全的。您必须使用 firebase 或其他方式来保护您的代码
    猜你喜欢
    • 2014-03-07
    • 2020-10-04
    • 1970-01-01
    • 2014-07-29
    • 2022-12-11
    • 2020-10-27
    • 2021-09-28
    • 2014-02-21
    • 2016-09-01
    相关资源
    最近更新 更多