|
|
|
@ -20,14 +20,21 @@ package cn.hippo4j.auth.service.impl;
|
|
|
|
|
import cn.hippo4j.auth.mapper.UserMapper;
|
|
|
|
|
import cn.hippo4j.auth.model.UserInfo;
|
|
|
|
|
import cn.hippo4j.auth.model.biz.user.JwtUser;
|
|
|
|
|
import cn.hippo4j.auth.model.biz.user.LoginUser;
|
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|
|
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
|
|
|
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
|
|
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
|
|
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
|
|
|
import org.springframework.web.context.request.RequestAttributes;
|
|
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.Set;
|
|
|
|
@ -38,11 +45,18 @@ import java.util.Set;
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class UserDetailsServiceImpl implements UserDetailsService {
|
|
|
|
|
|
|
|
|
|
@Value("${hippo4j.core.auth.enabled:true}")
|
|
|
|
|
private Boolean enableAuthentication;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private UserMapper userMapper;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
|
|
|
|
|
JwtUser anonymous = dealWithAnonymous();
|
|
|
|
|
if (!Objects.isNull(anonymous)) {
|
|
|
|
|
return anonymous;
|
|
|
|
|
}
|
|
|
|
|
UserInfo userInfo = userMapper.selectOne(Wrappers.lambdaQuery(UserInfo.class).eq(UserInfo::getUserName, userName));
|
|
|
|
|
if (Objects.isNull(userInfo)) {
|
|
|
|
|
log.warn("User {} not found", userName);
|
|
|
|
@ -56,4 +70,27 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
|
|
|
|
jwtUser.setAuthorities(authorities);
|
|
|
|
|
return jwtUser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private JwtUser dealWithAnonymous() {
|
|
|
|
|
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
|
|
|
|
|
if (requestAttributes == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
|
|
|
|
|
LoginUser loginUser = (LoginUser) request.getAttribute("loginUser");
|
|
|
|
|
if (Objects.isNull(loginUser)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (Boolean.FALSE.equals(enableAuthentication)) {
|
|
|
|
|
JwtUser jwtUser = new JwtUser();
|
|
|
|
|
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
|
|
|
|
|
jwtUser.setId(1L);
|
|
|
|
|
jwtUser.setUsername("anonymous");
|
|
|
|
|
jwtUser.setPassword(bCryptPasswordEncoder.encode(loginUser.getPassword()));
|
|
|
|
|
Set<SimpleGrantedAuthority> authorities = Collections.singleton(new SimpleGrantedAuthority("ROLE_ADMIN"));
|
|
|
|
|
jwtUser.setAuthorities(authorities);
|
|
|
|
|
return jwtUser;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|