0
一些背景Spring Security認證是空的錯誤頁面重定向後
當我試圖建立與用戶的信息和角色訪問我遇到了一些問題,從驗證檢索404錯誤頁面上的信息一個導航欄(Spring Security)處理攔截器。
@Component
public class AuthenticationHandlerInterceptor implements HandlerInterceptor {
@Autowired
private IAuthenticationFacade authenticationFacade;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
if (modelAndView != null) {
Authentication authentication = authenticationFacade.getAuthentication(); // <- Unexpected Null
if (authentication != null && !(authentication instanceof AnonymousAuthenticationToken)) {
modelAndView.getModel().put("user",
new UserModel(
authentication.getName(),
AuthorityUtils.authorityListToSet(authentication.getAuthorities())
)
); //put
} //fi
}//fi
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {}
}
IAuthenticationFacade被實現爲baeldung建議。
問題
後,我與一些用戶進行身份驗證,所有的頁面請求都將通的postHandle並有可能獲得當前用戶數據。除了頁面被重定向到404之外。
以下行返回null。
Authentication authentication = authenticationFacade.getAuthentication();
錯誤重定向
@Configuration
public class ServerPropertiesConfig extends ServerProperties {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
super.customize(container);
container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/errors/404/"));
}
}
當我訪問某些網頁像本地主機/不存在頁/它會被重定向到本地主機/錯誤/ 404/和處理在以下控制器上:
@Controller
@RequestMapping(value = "/errors")
public class ErrorController {
@RequestMapping(value = "/{error}/")
public String errorRedirect(@PathVariable String error) {
return "errors/error" + error;
}
}
你到底在哪裏?在ErrorController,ServerPropertiesConfig或AuthenticationHandlerInterceptor? –