| | |
| | | |
| | | import com.genersoft.iot.vmp.conf.security.dto.LoginUser; |
| | | import com.genersoft.iot.vmp.storager.dao.dto.User; |
| | | import gov.nist.javax.sip.address.UserInfo; |
| | | import org.springframework.security.authentication.AuthenticationManager; |
| | | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| | | import org.springframework.security.core.Authentication; |
| | |
| | | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
| | | |
| | | import javax.security.sasl.AuthenticationException; |
| | | import java.time.LocalDateTime; |
| | | |
| | | public class SecurityUtils { |
| | | |
| | |
| | | public static LoginUser login(String username, String password, AuthenticationManager authenticationManager) throws AuthenticationException { |
| | | //使用security框架自带的验证token生成器 也可以自定义。 |
| | | UsernamePasswordAuthenticationToken token =new UsernamePasswordAuthenticationToken(username,password); |
| | | //认证 如果失败,这里会自动异常后返回,所以这里不需要判断返回值是否为空,确定是否登录成功 |
| | | Authentication authenticate = authenticationManager.authenticate(token); |
| | | SecurityContextHolder.getContext().setAuthentication(authenticate); |
| | | LoginUser user = (LoginUser) authenticate.getPrincipal(); |
| | | |
| | | SecurityContextHolder.getContext().setAuthentication(token); |
| | | |
| | | return user; |
| | | } |
| | | |
| | |
| | | Authentication authentication = getAuthentication(); |
| | | if(authentication!=null){ |
| | | Object principal = authentication.getPrincipal(); |
| | | if(principal!=null){ |
| | | LoginUser user = (LoginUser) authentication.getPrincipal(); |
| | | return user; |
| | | if(principal!=null && !"anonymousUser".equals(principal.toString())){ |
| | | |
| | | User user = (User) principal; |
| | | return new LoginUser(user, LocalDateTime.now()); |
| | | } |
| | | } |
| | | return null; |