【问题标题】:how to create logout session in spring?如何在春季创建注销会话?
【发布时间】:2014-03-27 13:47:46
【问题描述】:

1).我是 spring 技术的新手。所以我从登录和注销 webapp 开始。 2).我在jsp中创建了登录页面,还添加了web.xml和spring-servlet.xml。 现在。如果我想使用户的会话无效,我应该如何做以及应该在哪里发生更改,请帮助我解决这个问题...我正在发布登录控制器和所有页面。

   controller:
          @Controller
            public class AdminLoginController extends AbstractController 
            {

                static Logger log = Logger.getLogger(AdminLoginController.class.getName());

                @RequestMapping(value = "/loginForm", method ={RequestMethod.GET,RequestMethod.POST})
                   public ModelAndView showForm(ModelMap model) 
                   {
                    AdminLoginForm loginForm =  new AdminLoginForm();
                    model.put("loginForm", loginForm);  
                     log.info("Inside Controller returning to loginform page....");


                    return new ModelAndView( GlobalConstants.LOGIN_PAGE);
                   }

                @RequestMapping(value = "/login" ,method ={RequestMethod.POST, RequestMethod.GET})
                public ModelAndView processForm(@ModelAttribute("loginForm")AdminLoginForm loginForm, BindingResult result , HttpServletRequest request, HttpServletResponse response, ModelMap model)
                {

                try{
                    loginForm = (AdminLoginForm) model.get("loginForm");
                    String returnPage="";
                    model=super.execute(model);
                    if(result.hasErrors()){
                        return new ModelAndView(GlobalConstants.ERRORPAGE);
                    }

                    AdminLoginWorker worker=new AdminLoginWorker();
                    boolean status=worker.validateUser(loginForm);
                    if(status)
                    {                   
                        model.addObject("request", request);
                        HttpSession session=super.getSession(model);
                        CommonDTOBean dtoBean=(CommonDTOBean)session.getAttribute("dtoBean");

                        if("Admin".equalsIgnoreCase(loginForm.getUserType())){
                            dtoBean.setEmp_id(loginForm.getUserName());
                            dtoBean.setEmpType("Admin");
                            session.setAttribute("dtoBean", dtoBean);

                            return new ModelAndView(GlobalConstants.HOME_PAGE);
                        }else{
                            dtoBean.setEmp_id(loginForm.getUserName());
                            dtoBean.setEmpType("Employee");
                            session.setAttribute("dtoBean", dtoBean);
                            return new ModelAndView(GlobalConstants.EMP_HOME_PAGE);
                        }
                    }
                    else
                    {
                        return new ModelAndView(GlobalConstants.LOGIN_PAGE);
                    }

                }catch(Exception e){

                    e.printStackTrace();
                }
                return new ModelAndView(GlobalConstants.LOGIN_PAGE);
                }
 and spring-servlet.xml is:

 <context:component-scan base-package="com.portal.controller" />
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>
   <bean id="messageSource"  class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages"/>     
        <property name="defaultEncoding" value="UTF-8"/>
  </bean>

    <bean id="localeChangeInterceptor"
        class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>

    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="en"/>
    </bean> 
        <bean id="handlerMapping"
              class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
            <property name="interceptors">
                <ref bean="localeChangeInterceptor" />
            </property>
     </bean>
</beans>

1.是否需要创建securityContentxt.xml进行注销。 2.上面的控制器类扩展了一些验证会话是否为空的抽象类。

请帮我解决这个问题,我浏览了http://docs.spring.io/spring-security/site/docs/3.0.x/reference/ns-config.html#ns-session-mgmt 网站但不明白。

我已经尝试了从这里得到的解决方案,但无法解决。我已经配置了 spring-security.xml:

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.0.xsd">
    <http auto-config="true">
    <intercept-url pattern="/loginPage" access="IS_AUTHENTICATED_ANONYMOUSLY" />

            <logout logout-success-url="/errorPage" logout-url="//errorPage"/>
            <session-management invalid-session-url="/home?invalid=true" />
        </http>

我在 web.xml 中添加了以下代码:

<filter>
  <filter-name>springSecurityFilterChain</filter-name>
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
  <filter-name>springSecurityFilterChain</filter-name>
  <url-pattern>*.do</url-pattern>
</filter-mapping>

然后我收到此错误:

java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:251)

谁能告诉我这有什么问题?

【问题讨论】:

标签: spring


【解决方案1】:

对于注销一个简单的链接,如

<a href="/j_spring_security_logout">Logout</a>

应该足以启动注销控制器。

关于您的其余问题,我真的不明白登录部分是否正常工作,而您只是需要注销,或者您是否需要整个系统的帮助。一旦登录成功并且您将用户正确存储在会话中,您无需做任何其他事情,Spring 安全性会管理一切。如果您需要更多帮助,请提供更多信息,我很乐意为您提供帮助!

干杯

【讨论】:

  • 非常感谢您的回答。如果我将上面的代码放在jsp页面上而不添加任何xml文件,它会起作用吗。是否需要为servelt .xml文件添加bean id .
  • 如果您的登录代码确实有效,那么是的,您无需添加任何其他内容。但是,您必须确保正确配置了 spring 安全性!
  • 不,我没有配置弹簧安全属性。实际上我不知道如何做到这一点,我的意思是我确实通过了教程但什么都不懂。
  • 那么你应该首先学会正确配置安全上下文,在这种情况下是的,你需要使用 .xml 文件来实际告诉 Spring Security 它必须做什么。用两个词来解释你必须做什么并不容易。我第一次发现mkyong的教程很有用[link]mkyong.com/spring-security/spring-security-hello-world-example/…尝试一步一步看它不着急!
  • 现在我明白了,如何创建注销?但是是否可以像管理员一样以不同角色登录,管理和用户如何做到这一点?
【解决方案2】:

在您的 jsp 中这有效: &lt;a href="&lt;c:url value="/j_spring_security_logout"/&gt;"&gt;Logout&lt;/a&gt;

或者如果您需要从其他来源执行此操作,请在 java 中编程

SecurityContextHolder.clearContext();

并删除会话:

HttpSession session = request.getSession(false);
if (session != null) {
  session.invalidate();
}

【讨论】:

  • 请告诉我如何配置上面的servlet.xml以获得spring安全性。
  • @user3264841 看看 github 上的示例 spring 应用程序之一
猜你喜欢
  • 1970-01-01
  • 2016-11-06
  • 2012-04-22
  • 1970-01-01
  • 2015-10-02
  • 1970-01-01
  • 2013-06-11
  • 2015-11-30
  • 1970-01-01
相关资源
最近更新 更多