简体   繁体   English

在Spring-Security中注销用户时从会话中删除用户登录凭据

[英]Removing user login credentials from session when user logout in spring-security

I am new to Spring and Spring-Security. 我是Spring和Spring-Security的新手。 I have been going through the tutorials here 我一直在这里看教程

The user are not allowed to hit add employee page without login. 未经登录,不允许用户点击添加员工页面。 So if you hit add employee page, you will be directed to the login page and when login succeeded you are directed to the add employee page automatically. 因此,如果您点击添加员工页面,您将被定向到登录页面,成功登录后,您将被自动引导到添加员工页面。

But once the user logged in add employee link can be accessed even after the user logs out. 但是,一旦用户登录,即使在用户注销后也可以访问添加员工链接。 It can be accessed even after the server is restarted, I had to close the browser window for the login credentials to be destroyed. 即使在重新启动服务器后也可以访问它,我不得不关闭浏览器窗口才能删除登录凭据。

It works fine when when I keep the logout url as "j_spring_security_logout" which I dont want to use. 当我将注销URL保留为我不想使用的“ j_spring_security_logout”时,它可以正常工作。 I want to use custom feild names and urls, is it possible? 我想使用自定义字段名称和网址,这可能吗?

This is how my spring-security.xml looks 这就是我的spring-security.xml的外观

<http auto-config="true" use-expressions="true">
    <intercept-url pattern="/login" access="permitAll" />
    <intercept-url pattern="/logout" access="permitAll" />
    <intercept-url pattern="/accessdenied" access="permitAll" />
    <intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
    <form-login login-page="/login" default-target-url="/list"
        authentication-failure-url="/accessdenied" />
    <logout logout-success-url="/logout" invalidate-session="true"
        delete-cookies="true" />
</http>
<authentication-manager alias="authenticationManager">
    <authentication-provider>
        <user-service>
            <user name="hasif" password="password" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
</authentication-manager>

This is how my LoginController looks 这就是我的LoginController的样子

@Controller public class LoginController { @Controller公共类LoginController {

@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login() {     
    return "login";
}

@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String logout(HttpServletRequest request) {
    HttpSession session = request.getSession(false);
    session.invalidate();
    return "logout";
}

@RequestMapping(value = "/accessdenied", method = RequestMethod.GET)
public String accessdenied() {
    return "accessdenied";
}

} }

Your configuration is wrong, you must specify the logout-url attribute and not the logout-success-url . 您的配置错误,必须指定logout-url属性而不是logout-success-url The latter is the url you are send to after logout has been successful. 后者是注销成功后发送到的URL。

<logout logout-url="/logout" invalidate-session="true" delete-cookies="true" />

delete-cookies takes a comma separated string with names of cookies to delete, I doubt you have a cookie named true and the session is invalidated by default. delete-cookies带有逗号分隔的字符串,其中包含要删除的cookie名称,我怀疑您有一个名为true的cookie,并且默认情况下该会话无效。 So basically the following gives the same result. 因此,基本上,以下给出了相同的结果。

<logout logout-url="/logout" />

If you want to change the name of the parameter to use for specifying the username/password add respectively the username-parameter and password-parameter on the form-login element. 如果要更改用于指定用户名/ password-parameter ,请在form-login元素上分别添加username-parameterpassword-parameter

<form-login login-page="/login" default-target-url="/list" authentication-failure-url="/accessdenied" username-parameter="my-username-param" password-parameter="my-password-param"/>

For an explanation of the namespaces I suggest a read of the reference guide . 有关名称空间的说明,建议阅读参考指南

First Why do you want to do manually when Spring provide such a good security feature. 首先,当Spring提供了这么好的安全功能时, Why do you want to do manually

No doubt you can handle it by your self. 毫无疑问,您可以自己处理。 For this you have to just invalidate the current session when user click on Log out button or link. 为此,您只需要在用户单击注销按钮或链接时invalidate当前会话invalidate There is method available HttpSession#invalidate() which can solve your problem. 有可用的HttpSession#invalidate()方法可以解决您的问题。

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

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