下面列出了怎么用org.springframework.security.web.savedrequest.RequestCache的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 登陆成功,返回Token 装配此bean不支持授权码模式
*
* @return
*/
@Bean
public AuthenticationSuccessHandler loginSuccessHandler() {
return new SavedRequestAwareAuthenticationSuccessHandler() {
private RequestCache requestCache = new HttpSessionRequestCache();
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
super.onAuthenticationSuccess(request, response, authentication);
return;
}
};
}
/**
* 登陆成功后的处理
*
* @return
*/
@Bean
public AuthenticationSuccessHandler successHandler(){
return new AuthenticationSuccessHandler(){
@Override
public void onAuthenticationSuccess( HttpServletRequest _request, HttpServletResponse _response, Authentication _auth ) throws IOException, ServletException{
log.debug( "--登陆成功" );
_request.getSession().setAttribute( ISupportEL.Session_User_EL, _auth.getPrincipal() );
sessionRegistry().registerNewSession( _request.getSession().getId(), _auth.getPrincipal() );
RequestCache requestCache = new HttpSessionRequestCache();
SavedRequest savedRequest = requestCache.getRequest( _request, _response );
String url = null;
if( savedRequest != null ) url = savedRequest.getRedirectUrl();
log.debug( "--登陆后转向:" + url );
if( url == null ) redirectStrategy().sendRedirect( _request, _response, "/" );
else _response.sendRedirect( url );
}
};
}
private RequestCache newHttpSessionRequestCache() {
HttpSessionRequestCache httpSessionRequestCache = new HttpSessionRequestCache();
httpSessionRequestCache.setCreateSessionAllowed(false);
return httpSessionRequestCache;
}
public RequestCache getRequestCache() {
return requestCache;
}
public void setRequestCache(RequestCache requestCache) {
this.requestCache = requestCache;
}
public void setRequestCache(RequestCache requestCache) {
this.requestCache = requestCache;
}
public void setRequestCache(RequestCache requestCache) {
this.requestCache = requestCache;
}
public RealAuthenticationFailureHandler(RequestCache requestCache) {
super();
this.requestCache = requestCache;
}
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler)
throws Exception {
_logger.trace("PermissionAdapter preHandle");
//save first protected url
SavedRequest firstSavedRequest = (SavedRequest)WebContext.getAttribute(WebConstants.FIRST_SAVED_REQUEST_PARAMETER);
// 判断用户是否登录, 判断用户和角色,判断用户是否登录用户
if (WebContext.getAuthentication() == null
|| WebContext.getAuthentication().getAuthorities() == null) {
//保存未认证的请求信息
if(firstSavedRequest==null){
RequestCache requestCache = new HttpSessionRequestCache();
requestCache.saveRequest(request, response);
SavedRequest savedRequest =requestCache.getRequest(request, response);
if(savedRequest!=null){
_logger.debug("first request parameter savedRequest "+savedRequest.getRedirectUrl());
WebContext.setAttribute(WebConstants.FIRST_SAVED_REQUEST_PARAMETER, savedRequest);
savedRequestSuccessHandler.setRequestCache(requestCache);
}
}
_logger.trace("No Authentication ... forward to /login");
RequestDispatcher dispatcher = request.getRequestDispatcher("/login");
dispatcher.forward(request, response);
return false;
}
//认证完成,跳转到未认证请求
if(firstSavedRequest!=null) {
savedRequestSuccessHandler.onAuthenticationSuccess(request, response, WebContext.getAuthentication());
WebContext.removeAttribute(WebConstants.FIRST_SAVED_REQUEST_PARAMETER);
}
boolean hasAccess = true;
/*
* boolean preHandler = super.preHandle(request, response, handler);
*
* if(preHandler) { preHandler = false;
*
*
* if(!preHandler){//无权限转向
* log.debug("You do not have permission to access "+accessUrl);
* RequestDispatcher dispatcher = request.getRequestDispatcher("/accessdeny");
* dispatcher.forward(request, response); return false; } }
*/
return hasAccess;
}
public void setRequestCache(RequestCache requestCache) {
this.requestCache = requestCache;
}
public void setRequestCache(RequestCache requestCache) {
this.requestCache = requestCache;
}
@Inject
public SimpleSignInAdapter(RequestCache requestCache) {
this.requestCache = requestCache;
}
public void setRequestCache(RequestCache requestCache) {
this.requestCache = requestCache;
}
public void setRequestCache(final RequestCache requestCache) {
this.requestCache = requestCache;
}
public void setRequestCache(final RequestCache requestCache) {
this.requestCache = requestCache;
}
public void setRequestCache(final RequestCache requestCache) {
this.requestCache = requestCache;
}
public void setRequestCache(final RequestCache requestCache) {
this.requestCache = requestCache;
}
public void setRequestCache(final RequestCache requestCache) {
this.requestCache = requestCache;
}