简体   繁体   English

春季安全。 删除一个拦截URL后不会重定向到登录页面

[英]Spring security. Doesn't redirect to login page after drop one intercept-url

I have the following spring-security configuration: 我具有以下spring-security配置:

<http auto-config="true" pattern="/admin/**" authentication-manager-ref="adminAuthenticationManager">
        <form-login login-page="/loginAdmin" login-processing-url="/admin/j_spring_security_check_admin"
                    default-target-url="/admin"
                    authentication-failure-url="/loginAdminFailed"
                    authentication-success-handler-ref="authAdminSuccessHandler"/>

        <intercept-url pattern="/admin/**"
                       access="ROLE_SUPERADMIN, ROLE_TERMINAL_MODERATOR, ROLE_IMAGE_MODERATOR, ROLE_CAMPAIGN_MODERATOR, ROLE_FINANSIER, ROLE_MODERATOR"/>


        <logout logout-url="/logout" logout-success-url="/loginAdmin"/>
        <port-mappings>
            <port-mapping http="${http.port}" https="${https.port}"/>
        </port-mappings>
    </http>

Now when I anonymous and attempt to go to the http://localhost:8080/admin 现在,当我匿名并尝试转到http://localhost:8080/admin
It redirects me to the http://localhost:8080/loginAdmin 它将我重定向到http://localhost:8080/loginAdmin

In debug I see that following controller method: 在调试中,我看到以下控制器方法:

@RequestMapping(value = "/admin", method = RequestMethod.GET)
    public String index(Principal principal, HttpSession session) {
        session.setAttribute("userName", principal.getName());
        return "admin/index";
}

doesn't invoke. 不调用。

when I removed 当我删除

<intercept-url pattern="/admin/**"
                       access="ROLE_SUPERADMIN, ROLE_TERMINAL_MODERATOR, ROLE_IMAGE_MODERATOR, ROLE_CAMPAIGN_MODERATOR, ROLE_FINANSIER, ROLE_MODERATOR"/>

from configuration I see the following situation: I type http://localhost:8080/admin - program execution goes to the controller method and I see NullPointer exception. 从配置中,我看到以下情况:输入http://localhost:8080/admin程序执行转到控制器方法,并且看到NullPointer异常。 Expected result - redirect to http://localhost:8080/loginAdmin 预期结果-重定向到http://localhost:8080/loginAdmin

Can you explain what happens ? 你能解释会发生什么吗?
why does my small changes has this side effect ? 为什么我的零钱会有这种副作用?

Usually there is a concept called session validation for authentication. 通常有一个称为会话验证的概念进行身份验证。 By default the web server will create unique session id in the cookie name JSESSIONID and append it in the HttpResponse and stores in the client browsers memory. 默认情况下,Web服务器将在cookie名称JSESSIONID创建唯一的会话ID,并将其附加在HttpResponse并存储在客户端浏览器的内存中。 If a user login to a web application the web server should invalidate the current session id from the login page(/loginadmin as in the above code) and it should create a new session id and appends in the browser's cookie name JSESSIONID based on every request sent from the client browser the web application should check whether the session is not new for the authenticated user and then it allows to execute the code inside the controller function. 如果用户登录到Web应用程序,则Web服务器应使登录页面中的当前会话ID无效(如上代码中的/ loginadmin),并且应创建一个新的会话ID,并根据每个请求将其添加到浏览器的cookie名称JSESSIONID 。从客户端浏览器发送的Web应用程序应检查该会话对于经过身份验证的用户而言是否不是新会话,然后允许在控制器功能内执行代码。

After logged out the session id should be invalidated and again assign a new unique session ID. 注销后,会话ID应该无效,然后再次分配一个新的唯一会话ID。 This technique is followed to prevent session hijacking attack . 遵循此技术可防止session hijacking attack So when the logged out user or any anonymous user trying to navigate to any url in the middle without authentication the server should validate if that is valid session then allows to execute the function further otherwise redirect to login page url. 因此,当注销的用户或任何试图在未经身份验证的情况下导航到中间任何URL的匿名用户时,服务器应验证该会话是否有效,然后允许进一步执行该功能,否则将重定向到登录页面URL。

So in spring security you need not to implement these security related functionalities. 因此,在春季安全性中,您无需实现这些与安全性相关的功能。 It has this session validation functionality and some additional functionalities. 它具有此会话验证功能和一些其他功能。 You just mention the url of the pages and their user role, it will handle it. 您只需要提及页面的网址及其用户角色,它将对其进行处理。

Before working in web framework like spring, struts, JSF, etc. you should know the java Servlets completely. 在使用spring, struts, JSF, etc. Web框架之前spring, struts, JSF, etc.您应该完全了解Java Servlet。 This link may be useful http://www.journaldev.com/1907/java-servlet-session-management-tutorial-with-examples-of-cookies-httpsession-and-url-rewriting 此链接可能是有用的http://www.journaldev.com/1907/java-servlet-session-management-tutorial-with-examples-of-cookies-httpsession-and-url-rewriting

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM