| | |
| | | import javax.net.ssl.SSLContext; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.ruoyi.common.config.SsoBean; |
| | | import com.ruoyi.common.core.domain.entity.User; |
| | | import com.ruoyi.framework.model.SsoOauthTokenModel; |
| | | import com.ruoyi.framework.model.SsoUserInfoModel; |
| | | import com.ruoyi.framework.web.ssoAuth.SsoCodeAuthenticationToken; |
| | | import com.ruoyi.system.mapper.UserMapper; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.http.HttpEntity; |
| | | import org.apache.http.HttpStatus; |
| | |
| | | private ISysConfigService configService; |
| | | |
| | | @Autowired |
| | | private SsoBean ssoBean; |
| | | |
| | | @Autowired |
| | | private RedisTemplate redisTemplate; |
| | | |
| | | /** |
| | |
| | | */ |
| | | public String login(String username, String password, String code, String uuid) |
| | | { |
| | | // 验证码校验 |
| | | validateCaptcha(username, code, uuid); |
| | | // // 验证码校验 |
| | | // validateCaptcha(username, code, uuid); |
| | | // 登录前置校验 |
| | | loginPreCheck(username, password); |
| | | // 用户验证 |
| | |
| | | recordLoginInfo(loginUser.getUserId()); |
| | | |
| | | // 创建登录信息 |
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(userInfo.getEmployeeId(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.single.login.success"))); |
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(userInfo.getEmployeeId(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.sso.login.success"))); |
| | | redisTemplate.opsForValue().set("ssoOauthToken:idToken:" + userInfo.getEmployeeId(), tokenModel.getId_token(), 3, TimeUnit.DAYS); |
| | | // 生成token |
| | | return tokenService.createToken(loginUser); |
| | |
| | | Authentication authentication = null; |
| | | SysUser sysUser = sysUserService.selectUserByUserName(username); |
| | | if (sysUser == null) { |
| | | throw new ServiceException("用户不存在"); |
| | | throw new ServiceException("当前系统没有该用户"); |
| | | } |
| | | try |
| | | { |
| | |
| | | { |
| | | if (e instanceof BadCredentialsException) |
| | | { |
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match"))); |
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.sso.not.match"))); |
| | | throw new UserPasswordNotMatchException(); |
| | | } |
| | | else |
| | |
| | | { |
| | | AuthenticationContextHolder.clearContext(); |
| | | } |
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"))); |
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.sso.login.success"))); |
| | | return (LoginUser) authentication.getPrincipal(); |
| | | |
| | | } |
| | |
| | | * @return |
| | | */ |
| | | public SsoOauthTokenModel getSsoAccessToken(String code) { |
| | | List<NameValuePair> list = new LinkedList<>(); |
| | | list.add(new BasicNameValuePair("grant_type", "authorization_code")); |
| | | list.add(new BasicNameValuePair("code", code)); |
| | | list.add(new BasicNameValuePair("client_id", ssoBean.getClientId())); |
| | | list.add(new BasicNameValuePair("client_secret", ssoBean.getClientSecret())); |
| | | list.add(new BasicNameValuePair("redirect_uri", ssoBean.getCallbackUrl())); |
| | | |
| | | Map<String, String> headers = new HashMap<>(); |
| | | headers.put("Content-Type", "application/x-www-form-urlencoded"); |
| | | headers.put("Accept", "application/json"); |
| | | String result = doPost(ssoBean.getUrl() + "/oauth2/token", headers, list); |
| | | if (org.apache.commons.lang3.StringUtils.isBlank(result)) { |
| | | return null; |
| | | } |
| | | return JSON.parseObject(result, SsoOauthTokenModel.class); |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return |
| | | */ |
| | | public SsoUserInfoModel getSsoUserInfo(String accessToken) { |
| | | Map<String, String> headers = new HashMap<>(); |
| | | headers.put("Authorization", "Bearer " + accessToken); |
| | | String result = doPost(ssoBean.getUrl() + "/userinfo", headers, null); |
| | | if (org.apache.commons.lang3.StringUtils.isBlank(result)) { |
| | | return null; |
| | | } |
| | | return JSON.parseObject(result, SsoUserInfoModel.class); |
| | | return null; |
| | | } |
| | | |
| | | public static String doPost(String url, Map<String, String> headers, List<NameValuePair> params) { |