限制账号的会话管理方案
MySessionInformationExpiredStrategy
package com.jiangzhu.auth;
import com.jiangzhu.api.Result;
import com.jiangzhu.api.ResultCode;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 账号被挤下线的处理方案
*/
@Component
public class MySessionInformationExpiredStrategy extends JSONAuthentication implements SessionInformationExpiredStrategy {
@Override
public void onExpiredSessionDetected(SessionInformationExpiredEvent sessionInformationExpiredEvent) throws IOException, ServletException {
//生成request
HttpServletRequest request = sessionInformationExpiredEvent.getRequest();
//生成response
HttpServletResponse response = sessionInformationExpiredEvent.getResponse();
Result result=Result.error(ResultCode.USER_ACCOUNT_USE_BY_OTHERS);
this.WriteJSON(request,response,result);
}
}
WebSecurityConfig
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true,prePostEnabled = true) //