First Commit

This commit is contained in:
Administrator
2025-05-12 12:04:42 +08:00
commit 6a5e13974c
1248 changed files with 366157 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
package quant.rich.emoney.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import jakarta.servlet.http.HttpSession;
import quant.rich.emoney.entity.config.PlatformConfig;
import quant.rich.emoney.util.SpringContextHolder;
@Service
public class AuthService implements UserDetailsService {
public static final String LOGIN_USER = "loginUser";
public static final String SPRING_SECURITY_CONTEXT = "SPRING_SECURITY_CONTEXT";
public static final String CAPTCHA = "captcha";
@Autowired
PlatformConfig platformConfig;
@Autowired
HttpSession session;
public Boolean isLogin() {
return session != null && session.getAttribute(SPRING_SECURITY_CONTEXT) != null;
}
public void setLogin(String username, String password) {
AuthenticationManager authenticationManager = SpringContextHolder.getBean("authenticationManager");
Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(username, password)
);
SecurityContextHolder.getContext().setAuthentication(authentication);
session.setAttribute(SPRING_SECURITY_CONTEXT, SecurityContextHolder.getContext());
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
if (StringUtils.isNotBlank(username) && username.equals(platformConfig.getUsername())) {
return
User.withUsername(username)
.password(platformConfig.getPassword())
.roles("admin")
.build();
}
throw new UsernameNotFoundException("用户不存在");
}
}