Search code examples
javasecurityshiro

Different Form login in Shiro


Same question where asked under "Multiple logins url in Shiro" but it was unanswered.

I am trying to have two different loggin page for apache shiro 1) For Mobile devices (little screen, limited javascript different theme) 2) For standard devices

How can i do?

Now only one url is working at a time.

shiro.loginUrl = ...

Thankyou


Solution

  • You need to extend FormAuthenticationFilter to be aware of multiple login urls. It can look like this:

    public class CustomAuthenticationFilter extends FormAuthenticationFilter {
        private Map<String, String> loginUrlByUserAgent = new HashMap<String, String>();
    
        public void setLoginUrls(final Map<String, String> loginUrlByUserAgent) {
            this.loginUrlByUserAgent = loginUrlByUserAgent;
        }
    
        protected void redirectToLogin(final ServletRequest request, final ServletResponse response) throws IOException {
            final String loginUrl = getLoginUrl(request);
            WebUtils.issueRedirect(request, response, loginUrl);
        }
    
        private String getLoginUrl(final ServletRequest request) {
            // check user agent
            final String userAgent = getUserAgent(request);
            // and return appropriate login url
            return userAgent != null && loginUrlByUserAgent.containsKey(userAgent) ?
                    loginUrlByUserAgent.get(userAgent) :
                    getLoginUrl();
        }
    
        private String getUserAgent(final ServletRequest request) {
            // get "User-Agent" header
        }
    }
    

    Then you just need to replace authc filter with your newly created.