【问题标题】:How to made Spring Security to redirect to specified path after Authentation如何使 Spring Security 在身份验证后重定向到指定路径
【发布时间】:2016-05-04 13:16:15
【问题描述】:

这是我第一次尝试 Spring Security,所以我的问题的最终解决方案可能很简单。所以... 我有一个索引页面,它只有登录表单:

<form name="loginForm" action="login.html" method="post">
<table>
    <tr>
        <td><@spring.message "form.email" /></td>
        <td><input type="text" name="email" /></td>
    </tr>
    <tr>
        <td><@spring.message "form.password" /></td>
        <td><input type="password" name="password" /></td
    </tr>
</table>
    <input type="submit" value="${signIn}" />
</form>
<form name="" action="createAccount.html">
    <input type="submit" value="${register}" />
</form>

当我发出 POST 请求时,它由控制器处理。在我的控制器中,我从数据库中检索 UserAccount 数据并将其传递到另一个名为“account.html”的页面。

这个控制器方法贴在下面:

@RequestMapping(value = "/login", method = RequestMethod.POST)
    public String logUserAccount(@RequestParam(value = "email", required = false) String email,
            @RequestParam(value = "password", required = false) String password, RedirectAttributes redirect) {
        try {

            UserAccount userAccount = userAccountService.signIn(email, password);
            redirect.addFlashAttribute("userAccount", userAccount);

            return "redirect:account.html";

        } catch (InvalidCreditnailsException e) {
            return RedirectController.REDIRECT_TO_INDEX_VIEW;
        }
    }

和 Next 控制器方法,将用户帐户数据建模并渲染 account.html 页面:

@RequestMapping(value = "/account", method = RequestMethod.GET)
    public String accountWindow(@ModelAttribute("userAccount") UserAccount userAccount, Model model){
        model.addAttribute("userAccount", userAccount);
        return "account";
    }

现在,我想保护 account.html 页面,防止非授权用户直接访问 /account.html 页面。但是我的 Spring Security 配置不正确。它看起来像这样:

<security:http>
        <security:intercept-url pattern="/account**" access="ROLE_USER" />
        <security:form-login
            login-page="/index.html"
            login-processing-url="/login.html"
            default-target-url="/account.html"
            username-parameter="email"
            password-parameter="password" 
         />
        <security:logout />
    </security:http>

    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user name="test@gmail.com" password="qwerty" authorities="ROLE_USER"/>
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>

它实际上是做什么的?当我尝试直接访问 /account.html 时,它会将我重定向到我有登录表单的 index.html。没关系。但是当我登录 Spring Security 时直接将我重定向到 /account.html 页面,而不是向我的登录控制器发送 /login.html 请求以检索用户数据。

如何设置> 有什么想法吗?也许我的方法不正确?我只希望所有客人都可以使用索引和注册页面。页面的其余部分仅适用于登录用户。

感谢您的帮助。

【问题讨论】:

    标签: spring spring-mvc spring-security


    【解决方案1】:

    听起来您需要从当前上下文中获取您的用户帐户,因为您已经登录。例如:您登录,关闭浏览器,打开一个新的浏览器窗口,然后导航到 /account.html。您应该仍然可以访问 /account.html,因为您在服务器上有一个活动会话。

    您可能需要考虑在 AccountController 中执行以下操作:

    检索经过身份验证的用户:

    Authentication auth = SecurityContextHolder().getContext().getAuthentication();
    

    获取用户的用户名:

    String username = auth.getName();
    

    获取用户的账户对象:

    UserAccount userAccount = userService.getAccount(username);
    

    或者,您可以将 Principal 参数添加到 AccountController 以自动注入 Principal。

    @RequestMapping(value = "/account", method = RequestMethod.GET)
    public String accountWindow(Model model, Principal principal){
        UserAccount userAccount = userService.getAccount(principal.getName());
        model.addAttribute("userAccount", userAccount);
        return "account";
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    • 2017-01-24
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    相关资源
    最近更新 更多