若依、CAS集成

1.pom引入

ruoyi-framework module的pom中添加如下dependency


	org.jasig.cas.client
    cas-client-support-springboot
    3.6.4

2.添加cas相关的yml配置

在ruoyi-admin的resources中添加cas地址yml配置

# cas地址
cas:
    server-url-prefix: https://cas.com(需替换)/cas

3.添加Java代码

ruoyi-framework的config目录中添加CasConfig类

package com.ruoyi.framework.config;

import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.jasig.cas.client.validation.Cas20ServiceTicketValidator;
import org.jasig.cas.client.validation.TicketValidator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class CasConfig {
  @Value("${cas.server-url-prefix:localhost}")
  private String serverUrlPrefix;

  @Bean
  public TicketValidator ticketValidator() {
    if (StringUtils.isEmpty(serverUrlPrefix)) {
      log.error("cas ticketValidator init error.");
      throw new RuntimeException("cas ticketValidator init error.");
    }
    return new Cas20ServiceTicketValidator(serverUrlPrefix);
  }
}

ruoyi-admin的controller中添加CasLoginController

package com.ruoyi.web.controller.cas;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.CasLoginBody;
import com.ruoyi.framework.web.service.CasLoginService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@RestController
@Slf4j
public class CasLoginController extends BaseController {

  @Autowired private CasLoginService casLoginService;

  @PostMapping("/cas/login")
  @ApiOperation(
      value = "cas单点登录",
      httpMethod = "POST",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public AjaxResult login(@RequestBody @Valid CasLoginBody loginBody) {
    AjaxResult ajax = AjaxResult.success();
    String token = casLoginService.login(loginBody);
    ajax.put(Constants.TOKEN, token);
    return ajax;
  }
}

ruoyi-framework的service中添加CasLoginService

package com.ruoyi.framework.web.service;

import com.ruoyi.common.core.domain.model.CasLoginBody;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.jasig.cas.client.validation.Assertion;
import org.jasig.cas.client.validation.TicketValidationException;
import org.jasig.cas.client.validation.TicketValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class CasLoginService {
  @Autowired TicketValidator ticketValidator;
  @Autowired private TokenService tokenService;
  @Autowired private UserDetailsService userDetailsService;

  public String login(CasLoginBody casLoginBody) {
    try {
      Assertion assertion =
          ticketValidator.validate(casLoginBody.getTicket(), casLoginBody.getService());
	  // TODO 获取loginUser的方法,可根据项目的情况重写loadUserByUsername
      LoginUser loginUser =
          (LoginUser) userDetailsService.loadUserByUsername(assertion.getPrincipal().getName());
      return tokenService.createToken(loginUser);
    } catch (TicketValidationException e) {
      log.error("======> cas login error", e);
      throw new BaseException("cas login error");
    }
  }
}

添加CasLoginBody 类

package com.ruoyi.common.core.domain.model;

import lombok.Data;

import javax.validation.constraints.NotEmpty;

@Data
public class CasLoginBody {
  @NotEmpty(message = "ticket不能为空")
  private String ticket; // cas 返回的ticket

  @NotEmpty(message = "service不能为空")
  private String service;
}

你可能感兴趣的:(spring,java,spring,boot)