配置单点登录

话不多说,直接上代码

yml文件配置:

purist:
  cas:
    url-prefix: https://www.baidu.com  //单点登录的地址
    server-name: 127.0.0.1    //本地服务地址
    trust-store:
    ignore-pattern:

 

拦截登录的类

@Slf4j
@Component
public class WebAccessInterceptor extends HandlerInterceptorAdapter {

    private static final Logger logger = LoggerFactory.getLogger(WebAccessInterceptor.class);

    @Override
    public boolean preHandle(final HttpServletRequest request,
                             final HttpServletResponse response,
                             final Object handler) throws Exception {
        HttpSession session = request.getSession();

        BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
        if (session != null) {
            // 检查session是否存在用户信息
            String userId = (String) session.getAttribute("SSO_USER_ID");
            if (StringUtils.isNotBlank(userId)) {
                return true;
            }
            // 获取单点登录用户名,并存入session
            final AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();
            if (principal != null) {
                userId = principal.getName();

                Service service = (OAService) factory.getBean("ServiceImpl");
                //校验用户是否存在
                Map userInfo = service.getUserInfo(userId);

                if(userInfo == null){
                    return false;
                }

                session.setAttribute("userName", userInfo.get("USERNAME"));
                session.setAttribute("userCode", userInfo.get("USERCODE"));
                session.setAttribute("inMail", userInfo.get("USERINMAIL"));
                session.setAttribute("outMail", userInfo.get("USEROUTMAIL"));

                session.setAttribute("SSO_USER_ID", userId);

                return true;
            }
        }
        // 没有登录信息,认证失败
        response.sendError(HttpStatus.FORBIDDEN.value(), "您没有权限查看");
        return false;
    }
}

 

单点登录配置完成

转载于:https://my.oschina.net/u/3941708/blog/2874014

你可能感兴趣的:(配置单点登录)