| File was renamed from src/main/java/org/springblade/auth/endpoint/BladeTokenEndPoint.java |
| | |
| | | package org.springblade.auth.endpoint; |
| | | |
| | | import com.wf.captcha.SpecCaptcha; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.SneakyThrows; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springblade.common.cache.CacheNames; |
| | | import org.springblade.core.cache.utils.CacheUtil; |
| | |
| | | import org.springblade.core.tool.support.Kv; |
| | | import org.springblade.core.tool.utils.StringUtil; |
| | | import org.springblade.core.tool.utils.WebUtil; |
| | | import org.springblade.modules.system.entity.UserInfo; |
| | | 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.oauth2.common.OAuth2AccessToken; |
| | | import org.springframework.security.oauth2.common.OAuth2RefreshToken; |
| | | import org.springframework.security.oauth2.provider.AuthorizationRequest; |
| | | import org.springframework.security.oauth2.provider.ClientDetailsService; |
| | | import org.springframework.security.oauth2.provider.token.TokenStore; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import org.springframework.web.servlet.ModelAndView; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import javax.servlet.http.HttpSession; |
| | | import java.io.IOException; |
| | | import java.time.Duration; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | import static org.springblade.core.cache.constant.CacheConstant.*; |
| | | |
| | |
| | | @Slf4j |
| | | @RestController |
| | | @AllArgsConstructor |
| | | public class BladeTokenEndPoint { |
| | | public class BladeTokenEndPoints { |
| | | |
| | | private final BladeRedis bladeRedis; |
| | | private final JwtProperties jwtProperties; |
| | | private final ClientDetailsService clientDetailsService; |
| | | private final TokenStore tokenStore; |
| | | |
| | | @Resource |
| | | private AuthenticationManager authenticationManager; |
| | | |
| | | /** |
| | | * 登录页面 |
| | | */ |
| | | @SneakyThrows |
| | | @GetMapping("/oauth/login") |
| | | public ModelAndView require(ModelAndView model) { |
| | | model.setViewName("login"); |
| | | return model; |
| | | public void require(HttpServletResponse response) { |
| | | response.setStatus(302); |
| | | response.sendRedirect("http://localhost:1888"); |
| | | } |
| | | // |
| | | // /** |
| | | // * 登录页面 |
| | | // */ |
| | | // @GetMapping("/oauth/login") |
| | | // public ModelAndView require(ModelAndView model) { |
| | | // model.setViewName("login"); |
| | | // return model; |
| | | // } |
| | | |
| | | |
| | | /** |
| | | * 自定义登录 |
| | | * @param username 用户名 |
| | | * @param password 密码 |
| | | * @return |
| | | */ |
| | | @PostMapping("/oauth/form") |
| | | @ApiOperation(value = "自定义登录") |
| | | public void login(HttpServletResponse response,String username, String password) throws IOException { |
| | | // 用户验证 authenticate方法会去调用自定义 myAuthenticationProvider 进行自定义校验 |
| | | Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password)); |
| | | //取出用户登录信息 |
| | | // UserInfo userInfo = (UserInfo) authentication.getPrincipal(); |
| | | //定义返回 map |
| | | Map<String, Object> map = new HashMap<>(); |
| | | //创建token |
| | | // map.put("token",tokenService.createToken(userInfo)); |
| | | //返回 |
| | | response.sendRedirect("http://localhost:9530/oauth/authorize?client_id=sword&response_type=code&redirect_uri=https://www.baidu.com"); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @GetMapping("/oauth/confirm_access") |
| | | public ModelAndView confirm(HttpSession session, ModelAndView model) { |
| | | Object auth = session.getAttribute("authorizationRequest"); |
| | | if (auth != null) { |
| | | AuthorizationRequest authorizationRequest = (AuthorizationRequest) auth; |
| | | model.addObject("client", clientDetailsService.loadClientByClientId(authorizationRequest.getClientId())); |
| | | model.addObject("principal", SecurityContextHolder.getContext().getAuthentication().getPrincipal()); |
| | | } |
| | | model.setViewName("confirm"); |
| | | // Object auth = session.getAttribute("authorizationRequest"); |
| | | // if (auth != null) { |
| | | // AuthorizationRequest authorizationRequest = (AuthorizationRequest) auth; |
| | | // model.addObject("client", clientDetailsService.loadClientByClientId(authorizationRequest.getClientId())); |
| | | // model.addObject("principal", SecurityContextHolder.getContext().getAuthentication().getPrincipal()); |
| | | // } |
| | | // model.setViewName("confirm"); |
| | | return model; |
| | | } |
| | | |