| | |
| | | |
| | | import com.alibaba.nacos.common.utils.StringUtils; |
| | | import io.jsonwebtoken.Claims; |
| | | import lombok.*; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.SneakyThrows; |
| | | import me.zhyd.oauth.log.Log; |
| | | import org.springblade.auth.constant.AuthConstant; |
| | | import org.springblade.auth.utils.TokenUtil; |
| | |
| | | import org.springblade.system.user.entity.UserInfo; |
| | | import org.springblade.system.user.enums.UserEnum; |
| | | import org.springblade.system.user.feign.IUserClient; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.security.core.GrantedAuthority; |
| | | import org.springframework.security.core.authority.AuthorityUtils; |
| | | import org.springframework.security.core.authority.GrantedAuthoritiesContainer; |
| | | import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| | | import org.springframework.security.core.userdetails.UserDetailsService; |
| | | import org.springframework.security.core.userdetails.UsernameNotFoundException; |
| | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.time.Duration; |
| | | import java.util.ArrayList; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.NoSuchElementException; |
| | | import java.util.function.Predicate; |
| | | import java.util.stream.Stream; |
| | | |
| | | /** |
| | | * 用户信息 |
| | |
| | | private String userName; |
| | | @Value("${user-info.passwrod}") |
| | | private String password; |
| | | @Value("#{'${user-info.ip}'.split(',')}") |
| | | private List<String> ips; |
| | | @Value("${user-info.id}") |
| | | private String id; |
| | | @Value("${ip-whitelist.ip-enable}") |
| | | private Boolean ipEnable; |
| | | @Value("#{'${ip-whitelist.ip}'.split(',')}") |
| | | private List<String> ips; |
| | | |
| | | @Override |
| | | @SneakyThrows |
| | |
| | | setFailCount(tenantId, username, count); |
| | | throw new UsernameNotFoundException(TokenUtil.USER_NOT_FOUND); |
| | | } |
| | | Log.debug(getIpAddress(request)); |
| | | //如果ip比对后get抛出异常No value present就直接抛异常结束登录 |
| | | try { |
| | | ips.stream().filter(s -> s.equals(getIpAddress(request))).findFirst().get(); |
| | | } catch (Exception e){ |
| | | throw new UserDeniedAuthorizationException(TokenUtil.IP_NOT_FOND); |
| | | if(ipEnable){ |
| | | Log.debug("当前访问IP:"+getIpAddress(request)); |
| | | try { |
| | | ips.stream().filter(s -> s.equals(getIpAddress(request))).findFirst().get(); |
| | | } catch (Exception e){ |
| | | throw new UserDeniedAuthorizationException(TokenUtil.IP_NOT_FOND); |
| | | } |
| | | } |
| | | |
| | | ArrayList<GrantedAuthority> authorities = new ArrayList<>(); |
| | |
| | | String ip = request.getHeader("x-forwarded-for"); |
| | | if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) { |
| | | // 多次反向代理后会有多个ip值,第一个ip才是真实ip |
| | | if( ip.indexOf(",")!=-1 && !ip.split(",")[0].equals("127.0.0.1")){ |
| | | ip = ip.split(",")[0]; |
| | | }else { |
| | | ip = ip.split(",")[1]; |
| | | } |
| | | ip = ip.split(",")[0]; |
| | | } |
| | | if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
| | | ip = request.getHeader("Proxy-Client-IP"); |