【问题标题】:Spring-security not working, need help understanding whySpring-security 不起作用,需要帮助了解原因
【发布时间】:2014-07-29 14:52:49
【问题描述】:

我正在尝试了解 Spring-security,但无需登录即可访问我的页面,我不明白为什么。

“安全”页面位于 WEB-INF/pages/secure 中,可使用http://localhost:8080/secret 访问。这应该不允许访问,但目前允许。

/在此处保护地图

@Controller
public class HelloWorld {

    ...

    @RequestMapping("/secret")
    public String showSecret(ModelMap model) {
        return "secure/secretPage";
    }

}

web.xml

...
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/applicationContext-security.xml
        /WEB-INF/springmvc-config.xml
    </param-value>
</context-param>
...

applicationContext-security.xml

<http auto-config="true">
    <form-login login-processing-url="/j_spring_security_check"
    login-page="/login"
    authentication-failure-url="/login?login_error=t"/>
    <logout logout-url="/j_spring_security_logout"/>
    <intercept-url pattern="/pages/secure/**" access="IS_AUTHENTICATED_FULLY" requires-channel="https"/>
    <intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
</http>

<authentication-manager alias="authenticationManager">
    <authentication-provider user-service-ref="userAccountDetailsService"/>
</authentication-manager>

userAccountDetailsS​​ervice

@Service("userAccountDetailsService") // enables component to be found to <component-scan/>
public class UserAccountDetailsService implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        throw new UsernameNotFoundException("Could not find user");
    }

}

/login 页面当前不存在。反正没有用户。我只想暂时禁止访问。

【问题讨论】:

    标签: spring-security


    【解决方案1】:

    您访问的 URL 是 /secure,而不是 /pages/secure,但在您的 Spring Security 配置中,您保护的是 /pages/secure/** 而不是 /secure/**。将拦截 URL 从/pages/secure/** 更改为/secure/**,然后重试。

    【讨论】:

    • 由于某种原因无法正常工作。但随后,secure 被放置在 /pages/secure 中。我尝试将其移至 /WEB-INF/secure(与 /WEB-INF/pages 相同级别),但由于某些原因 pages/ 被视为 root:HTTP 状态 404 - /WEB-INF/pages/secure/secretPage.jsp
    • 我将您的代码稍微简化后复制到了一个示例应用程序中,它运行良好。 Access the app 并单击页面上显示的链接。您将被重定向到登录页面(如预期的那样)。完整的源代码可在on Github 获得。您的配置中的其他地方似乎有错误。如果您仍然面临错误,请发布完整的 Spring MVC 和 Spring Security 配置以及完整文件夹结构的屏幕截图(不仅仅是在 WEB-INF 中)。
    • 我不知道问题出在哪里,但通过删除和重写所有配置文件已“解决”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    相关资源
    最近更新 更多