簡體   English   中英

Spring MVC自定義身份驗證

[英]Spring MVC custom authentification

我需要在Spring 3.2 MVC Web應用程序中包含Jasig CAS身份驗證。

目前,我有兩個jar(CAS核心客戶端和公司特定的擴展名),以及web.xml中的一堆過濾器。 CAS服務器重定向請求后,某些會話屬性將提供記錄的配置文件數據。

我正在使用Singleton服務對其進行處理。 調用此服務是棘手的部分。 我可以使用攔截器來在每次Controller調用中調用我的Auth Service嗎?

我很確定使用Spring Security會有更好的方法,但是我無法正確理解它應該是什么樣的。

我終於使用了攔截器。

@Component
public class InterceptorAuth extends HandlerInterceptorAdapter {
    @Autowired
    private IAuthService authService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //AuthService checks session for login attributes, given through CAS servlet
        //AuthBean contains login data ; if it's null, login has failed
        AuthBean authBean = authService.getAuthBean(request);
        String reqUri = request.getRequestURI();
        String serviceName = reqUri.substring(reqUri.lastIndexOf("/") + 1, reqUri.length());
        String action = request.getParameter("action") != null ? request.getParameter("action") : "";
        //If Login failed, redirect to Error Page (Auth Controller / Error Action)
        //We let through if requested URL is Error Page itself
        //(This allows us to use a dynamic page ; unless we'll use a static HTML page called directly from here)
        if (authBean == null && !serviceName.equals("authError") && !action.equals("errorAuth")) {
            ModelAndView mv = new ModelAndView("redirect:auth.ctrl?action=errorAuth"); 
            ModelAndViewDefiningException mvde = new ModelAndViewDefiningException(mv); 
            throw mvde; 
        }
        return super.preHandle(request, response, handler);
    }
}

必需的Spring XML配置

<mvc:interceptors>
  <bean class="com.demo.sample.interceptor.InterceptorAuth"></bean>
</mvc:interceptors> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM