Spring Security Oauth2.0 实现短信验证码登录示例

 更新时间:2018年01月12日 10:54:35   作者:冷冷gg  
本篇文章主要介绍了Spring Security Oauth2.0 实现短信验证码登录示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文介绍了Spring Security Oauth2.0 实现短信验证码登录示例,分享给大家,具体如下:

定义手机号登录令牌

/**
 * @author lengleng
 * @date 2018/1/9
 * 手机号登录令牌
 */
public class MobileAuthenticationToken extends AbstractAuthenticationToken {

  private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;

  private final Object principal;

  public MobileAuthenticationToken(String mobile) {
    super(null);
    this.principal = mobile;
    setAuthenticated(false);
  }

  public MobileAuthenticationToken(Object principal,
                   Collection<? extends GrantedAuthority> authorities) {
    super(authorities);
    this.principal = principal;
    super.setAuthenticated(true);
  }

  public Object getPrincipal() {
    return this.principal;
  }

  @Override
  public Object getCredentials() {
    return null;
  }

  public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
    if (isAuthenticated) {
      throw new IllegalArgumentException(
          "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
    }

    super.setAuthenticated(false);
  }

  @Override
  public void eraseCredentials() {
    super.eraseCredentials();
  }
}

手机号登录校验逻辑

/**
 * @author lengleng
 * @date 2018/1/9
 * 手机号登录校验逻辑
 */
public class MobileAuthenticationProvider implements AuthenticationProvider {
  private UserService userService;

  @Override
  public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    MobileAuthenticationToken mobileAuthenticationToken = (MobileAuthenticationToken) authentication;
    UserVo userVo = userService.findUserByMobile((String) mobileAuthenticationToken.getPrincipal());

    UserDetailsImpl userDetails = buildUserDeatils(userVo);
    if (userDetails == null) {
      throw new InternalAuthenticationServiceException("手机号不存在:" + mobileAuthenticationToken.getPrincipal());
    }

    MobileAuthenticationToken authenticationToken = new MobileAuthenticationToken(userDetails, userDetails.getAuthorities());
    authenticationToken.setDetails(mobileAuthenticationToken.getDetails());
    return authenticationToken;
  }

  private UserDetailsImpl buildUserDeatils(UserVo userVo) {
    return new UserDetailsImpl(userVo);
  }

  @Override
  public boolean supports(Class<?> authentication) {
    return MobileAuthenticationToken.class.isAssignableFrom(authentication);
  }

  public UserService getUserService() {
    return userService;
  }

  public void setUserService(UserService userService) {
    this.userService = userService;
  }
}

登录过程filter处理

/**
 * @author lengleng
 * @date 2018/1/9
 * 手机号登录验证filter
 */
public class MobileAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
  public static final String SPRING_SECURITY_FORM_MOBILE_KEY = "mobile";

  private String mobileParameter = SPRING_SECURITY_FORM_MOBILE_KEY;
  private boolean postOnly = true;

  public MobileAuthenticationFilter() {
    super(new AntPathRequestMatcher(SecurityConstants.MOBILE_TOKEN_URL, "POST"));
  }

  public Authentication attemptAuthentication(HttpServletRequest request,
                        HttpServletResponse response) throws AuthenticationException {
    if (postOnly && !request.getMethod().equals(HttpMethod.POST.name())) {
      throw new AuthenticationServiceException(
          "Authentication method not supported: " + request.getMethod());
    }

    String mobile = obtainMobile(request);

    if (mobile == null) {
      mobile = "";
    }

    mobile = mobile.trim();

    MobileAuthenticationToken mobileAuthenticationToken = new MobileAuthenticationToken(mobile);

    setDetails(request, mobileAuthenticationToken);

    return this.getAuthenticationManager().authenticate(mobileAuthenticationToken);
  }

  protected String obtainMobile(HttpServletRequest request) {
    return request.getParameter(mobileParameter);
  }

  protected void setDetails(HttpServletRequest request,
               MobileAuthenticationToken authRequest) {
    authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
  }

  public void setPostOnly(boolean postOnly) {
    this.postOnly = postOnly;
  }

  public String getMobileParameter() {
    return mobileParameter;
  }

  public void setMobileParameter(String mobileParameter) {
    this.mobileParameter = mobileParameter;
  }

  public boolean isPostOnly() {
    return postOnly;
  }
}

生产token 位置

/**
 * @author lengleng
 * @date 2018/1/8
 * 手机号登录成功,返回oauth token
 */
@Component
public class MobileLoginSuccessHandler implements org.springframework.security.web.authentication.AuthenticationSuccessHandler {
  private Logger logger = LoggerFactory.getLogger(getClass());
  @Autowired
  private ObjectMapper objectMapper;
  @Autowired
  private ClientDetailsService clientDetailsService;
  @Autowired
  private AuthorizationServerTokenServices authorizationServerTokenServices;

  @Override
  public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
    String header = request.getHeader("Authorization");

    if (header == null || !header.startsWith("Basic ")) {
      throw new UnapprovedClientAuthenticationException("请求头中client信息为空");
    }

    try {
      String[] tokens = extractAndDecodeHeader(header);
      assert tokens.length == 2;
      String clientId = tokens[0];
      String clientSecret = tokens[1];

      JSONObject params = new JSONObject();
      params.put("clientId", clientId);
      params.put("clientSecret", clientSecret);
      params.put("authentication", authentication);

      ClientDetails clientDetails = clientDetailsService.loadClientByClientId(clientId);
      TokenRequest tokenRequest = new TokenRequest(MapUtil.newHashMap(), clientId, clientDetails.getScope(), "mobile");
      OAuth2Request oAuth2Request = tokenRequest.createOAuth2Request(clientDetails);

      OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(oAuth2Request, authentication);
      OAuth2AccessToken oAuth2AccessToken = authorizationServerTokenServices.createAccessToken(oAuth2Authentication);
      logger.info("获取token 成功:{}", oAuth2AccessToken.getValue());

      response.setCharacterEncoding(CommonConstant.UTF8);
      response.setContentType(CommonConstant.CONTENT_TYPE);
      PrintWriter printWriter = response.getWriter();
      printWriter.append(objectMapper.writeValueAsString(oAuth2AccessToken));
    } catch (IOException e) {
      throw new BadCredentialsException(
          "Failed to decode basic authentication token");
    }
  }

  /**
   * Decodes the header into a username and password.
   *
   * @throws BadCredentialsException if the Basic header is not present or is not valid
   *                 Base64
   */
  private String[] extractAndDecodeHeader(String header)
      throws IOException {

    byte[] base64Token = header.substring(6).getBytes("UTF-8");
    byte[] decoded;
    try {
      decoded = Base64.decode(base64Token);
    } catch (IllegalArgumentException e) {
      throw new BadCredentialsException(
          "Failed to decode basic authentication token");
    }

    String token = new String(decoded, CommonConstant.UTF8);

    int delim = token.indexOf(":");

    if (delim == -1) {
      throw new BadCredentialsException("Invalid basic authentication token");
    }
    return new String[]{token.substring(0, delim), token.substring(delim + 1)};
  }
}

配置以上自定义

//**
 * @author lengleng
 * @date 2018/1/9
 * 手机号登录配置入口
 */
@Component
public class MobileSecurityConfigurer extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {
  @Autowired
  private MobileLoginSuccessHandler mobileLoginSuccessHandler;
  @Autowired
  private UserService userService;

  @Override
  public void configure(HttpSecurity http) throws Exception {
    MobileAuthenticationFilter mobileAuthenticationFilter = new MobileAuthenticationFilter();
    mobileAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
    mobileAuthenticationFilter.setAuthenticationSuccessHandler(mobileLoginSuccessHandler);

    MobileAuthenticationProvider mobileAuthenticationProvider = new MobileAuthenticationProvider();
    mobileAuthenticationProvider.setUserService(userService);
    http.authenticationProvider(mobileAuthenticationProvider)
        .addFilterAfter(mobileAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
  }
}

在spring security 配置 上边定一个的那个聚合配置

/**
 * @author lengleng
 * @date 2018年01月09日14:01:25
 * 认证服务器开放接口配置
 */
@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
  @Autowired
  private FilterUrlsPropertiesConifg filterUrlsPropertiesConifg;
  @Autowired
  private MobileSecurityConfigurer mobileSecurityConfigurer;

  @Override
  public void configure(HttpSecurity http) throws Exception {
    registry
        .antMatchers("/mobile/token").permissionAll()
        .anyRequest().authenticated()
        .and()
        .csrf().disable();
    http.apply(mobileSecurityConfigurer);
  }
}

使用

复制代码 代码如下:

curl -H "Authorization:Basic cGlnOnBpZw==" -d "grant_type=mobile&scope=server&mobile=17034642119&code=" http://localhost:9999/auth/mobile/token

源码

请参考gitee.com/log4j/

基于Spring Cloud、Spring Security Oauth2.0开发企业级认证与授权,提供常见服务监控、链路追踪、日志分析、缓存管理、任务调度等实现

整个逻辑是参考spring security 自身的 usernamepassword 登录模式实现,可以参考其源码。

验证码的发放、校验逻辑比较简单,方法后通过全局fiter 判断请求中code 是否和 手机号匹配集合,重点逻辑是令牌的参数

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 详解Java HashMap实现原理

    详解Java HashMap实现原理

    HashMap是基于哈希表的Map接口实现,提供了所有可选的映射操作,并允许使用null值和null建,不同步且不保证映射顺序。本文将记录一下研究HashMap实现原理。
    2017-01-01
  • JavaAgent的简单例子

    JavaAgent的简单例子

    这篇文章主要介绍了JavaAgent的简单例子,对JavaAgent感兴趣的同学,可以参考下
    2021-04-04
  • Java中双向链表详解及实例

    Java中双向链表详解及实例

    这篇文章主要介绍了Java中双向链表详解及实例的相关资料,需要的朋友可以参考下
    2017-04-04
  • Java开发Spark应用程序自定义PipeLineStage详解

    Java开发Spark应用程序自定义PipeLineStage详解

    这篇文章主要为大家介绍了Java开发Spark应用程序自定义PipeLineStage详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • springboot日志切面通用类实例详解

    springboot日志切面通用类实例详解

    这篇文章主要介绍了springboot日志切面通用类,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01
  • linux系统下查看jdk版本、路径及配置环境变量

    linux系统下查看jdk版本、路径及配置环境变量

    在Linux系统中,配置JDK环境变量是非常重要的,它可以让你在终端中直接使用Java命令,这篇文章主要给大家介绍了关于linux系统下查看jdk版本、路径及配置环境变量的相关资料,需要的朋友可以参考下
    2024-01-01
  • 三步轻松搭建springMVC框架

    三步轻松搭建springMVC框架

    这篇文章主要教大家三步轻松搭建springMVC框架,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • 用Java代码实现栈数据结构的基本方法归纳

    用Java代码实现栈数据结构的基本方法归纳

    这篇文章主要介绍了用Java代码实现栈数据结构的基本方法归纳,各种算法的实现也是ACM上经常出现的题目,是计算机学习的基本功,需要的朋友可以参考下
    2015-08-08
  • Gradle进阶使用结合Sonarqube进行代码审查的方法

    Gradle进阶使用结合Sonarqube进行代码审查的方法

    今天小编就为大家分享一篇关于Gradle进阶使用结合Sonarqube进行代码审查的方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • Alibaba Fastjson之超好用的JOSN解析库

    Alibaba Fastjson之超好用的JOSN解析库

    这篇文章主要介绍了Alibaba Fastjson之超好用的JOSN解析库,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10

最新评论