【问题标题】:@Controller is ignored because of spring security由于弹簧安全性,@Controller 被忽略
【发布时间】:2016-05-27 08:29:38
【问题描述】:

当我有如下弹簧安全设置时:

<http pattern="/ui/**" auto-config="true" authentication-manager-ref="authenticationManager">
        <intercept-url pattern="/ui/login*" access="isAnonymous() or hasRole('USER')" requires-channel="${web.channel}" />
        <intercept-url pattern="/ui/**" access="hasRole('USER')" requires-channel="${web.channel}" />

        <form-login login-page="/ui/login" login-processing-url="/ui/j_spring_security_check" default-target-url="/ui/dashboard" />
        <logout logout-url="/ui/logout" logout-success-url="/ui/login" />
</http>

我的控制器被忽略,这两种方法都不会被调用。我尝试在它们两个内部添加断点,但执行从未停止。我也尝试向它们添加 System.out.println() 并且没有任何内容写入系统输出,所以我很确定这两种方法要么没有被映射,要么从未被调用。

@Controller
public class MyController {

    @RequestMapping(value = { "/ui", "/ui/login"}, method = GET)
    public String indexNoTrailingSlash() {
        return index();
    }

    @RequestMapping(value = "/", method = GET)
    public String index() {
        return "redirect:/ui/dashboard";
    }
}

安全工作正常,多亏了 xml 中的 default-target-url="/ui/dashboard",我在成功登录后确实进入了我的仪表板页面。但是,我想要实现的是,如果用户已经登录,他也会被重定向到仪表板。我尝试关注this answer,这就是我需要控制器的目的。有没有办法映射这些方法,以便我可以使用我的控制器进行重定向或以某种方式仅使用弹簧安全性进行重定向?

谢谢:)

【问题讨论】:

  • 为了解决您的问题,我使用了重定向拦截器,请参阅我对类似问题的回答stackoverflow.com/a/18012060/280244
  • 你为什么使用@Controller?相反,您必须在您的 Rest 类中使用 @RestController 或将 @RequestMapping("/") 添加到类中

标签: java spring spring-mvc spring-security


【解决方案1】:

如果您包含的答案链接正是您要寻找的,请针对您的案例执行以下操作。

return new ModelAndView("redirect:/"); 

它将重定向到您的 index() 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-03
    • 2012-01-07
    • 2017-05-08
    • 2016-06-19
    • 2018-09-08
    • 1970-01-01
    • 2020-05-19
    • 2015-12-20
    相关资源
    最近更新 更多