cz.muni.pa165.carparkapp.configuration.AuthenticationHandler.java Source code

Java tutorial

Introduction

Here is the source code for cz.muni.pa165.carparkapp.configuration.AuthenticationHandler.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.pa165.carparkapp.configuration;

import java.io.IOException;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

/**
 *
 * @author coldfront
 */
public class AuthenticationHandler implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest hsr, HttpServletResponse hsr1, Authentication a)
            throws IOException, ServletException {
        Set<String> roles = AuthorityUtils.authorityListToSet(a.getAuthorities());
        if (roles.contains("ROLE_USER")) {
            hsr1.sendRedirect(hsr.getContextPath() + "/");
        }
        if (roles.contains("ROLE_ADMIN")) {
            hsr1.sendRedirect(hsr.getContextPath() + "/admin/");
        }
    }

}