Skip to content

Commit

Permalink
✨ spring-boot-demo-social 努力中
Browse files Browse the repository at this point in the history
  • Loading branch information
xkcoding committed Feb 21, 2019
1 parent 8e380fa commit b7be5c8
Show file tree
Hide file tree
Showing 17 changed files with 690 additions and 1 deletion.
64 changes: 63 additions & 1 deletion spring-boot-demo-social/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,81 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring.social.version>1.1.6.RELEASE</spring.social.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-config</artifactId>
<version>${spring.social.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-core</artifactId>
<version>${spring.social.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-security</artifactId>
<version>${spring.social.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-web</artifactId>
<version>${spring.social.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>

<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.xkcoding.social;

import com.xkcoding.social.config.SocialProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

/**
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.xkcoding.social.config;

import org.springframework.social.security.SocialAuthenticationFilter;
import org.springframework.social.security.SpringSocialConfigurer;

/**
* <p>
* 继承默认的社交登录配置,加入自定义的后处理逻辑
* </p>
*
* @package: com.xkcoding.social.config
* @description: 继承默认的社交登录配置,加入自定义的后处理逻辑
* @author: yangkai.shen
* @date: Created in 2019-02-21 14:01
* @copyright: Copyright (c) 2019
* @version: V1.0
* @modified: yangkai.shen
*/
public class DemoSocialConfigure extends SpringSocialConfigurer {
private String filterProcessUrl;

public DemoSocialConfigure(String filterProcessUrl) {
this.filterProcessUrl = filterProcessUrl;
}

@Override
protected <T> T postProcess(T object) {
SocialAuthenticationFilter socialAuthenticationFilter = (SocialAuthenticationFilter) super.postProcess(object);
socialAuthenticationFilter.setFilterProcessesUrl(filterProcessUrl);
return (T) socialAuthenticationFilter;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.xkcoding.social.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.social.security.SocialUser;
import org.springframework.social.security.SocialUserDetails;
import org.springframework.social.security.SocialUserDetailsService;
import org.springframework.stereotype.Service;

/**
* <p>
* 根据userId获取用户信息
* </p>
*
* @package: com.xkcoding.social.config
* @description: 根据userId获取用户信息
* @author: yangkai.shen
* @date: Created in 2019-02-21 14:41
* @copyright: Copyright (c) 2019
* @version: V1.0
* @modified: yangkai.shen
*/
@Service
public class DemoUserDetailService implements SocialUserDetailsService {
@Autowired
private PasswordEncoder passwordEncoder;

@Override
public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException {
return new SocialUser(userId, passwordEncoder.encode("123456"), AuthorityUtils.commaSeparatedStringToAuthorityList("xxx"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.xkcoding.social.config;

import lombok.Data;

/**
* <p>
* QQ 配置
* </p>
*
* @package: com.xkcoding.social.config
* @description: QQ 配置
* @author: yangkai.shen
* @date: Created in 2019-02-21 14:20
* @copyright: Copyright (c) 2019
* @version: V1.0
* @modified: yangkai.shen
*/
@Data
public class QQProperties {
/**
* 第三方应用标识
*/
private String providerId = "qq";

/**
* clientId
*/
private String clientId;

/**
* clientSecret
*/
private String clientSecret;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.xkcoding.social.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.social.UserIdSource;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository;
import org.springframework.social.security.AuthenticationNameUserIdSource;
import org.springframework.social.security.SpringSocialConfigurer;

import javax.sql.DataSource;

/**
* <p>
* 社交登录配置
* </p>
*
* @package: com.xkcoding.social.config
* @description: 社交登录配置
* @author: yangkai.shen
* @date: Created in 2019-02-21 13:52
* @copyright: Copyright (c) 2019
* @version: V1.0
* @modified: yangkai.shen
*/
@Configuration
@EnableSocial
@EnableConfigurationProperties({SocialProperties.class})
public class SocialConfig extends SocialConfigurerAdapter {
@Autowired
private DataSource dataSource;

@Autowired
private SocialProperties socialProperties;

@Override
public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
JdbcUsersConnectionRepository jdbcUsersConnectionRepository = new JdbcUsersConnectionRepository(dataSource, connectionFactoryLocator, Encryptors
.noOpText());
jdbcUsersConnectionRepository.setTablePrefix("demo_");
return jdbcUsersConnectionRepository;
}

@Bean
public SpringSocialConfigurer springSocialConfigurer() {
DemoSocialConfigure demoSocialConfigure = new DemoSocialConfigure(socialProperties.getFilterProcessUrl());
return demoSocialConfigure;
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

@Override
public UserIdSource getUserIdSource() {
return new AuthenticationNameUserIdSource();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.xkcoding.social.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* <p>
* 社交登录配置类
* </p>
*
* @package: com.xkcoding.social.config
* @description: 社交登录配置类
* @author: yangkai.shen
* @date: Created in 2019-02-21 14:12
* @copyright: Copyright (c) 2019
* @version: V1.0
* @modified: yangkai.shen
*/
@Data
@ConfigurationProperties(prefix = "demo.social")
public class SocialProperties {
private String filterProcessUrl = "/auth";

private QQProperties qq = new QQProperties();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.xkcoding.social.qq.api;

/**
* <p>
* 获取QQ用户信息接口
* </p>
*
* @package: com.xkcoding.social.qq.api
* @description: 获取QQ用户信息接口
* @author: yangkai.shen
* @date: Created in 2019-02-21 10:52
* @copyright: Copyright (c) 2019
* @version: V1.0
* @modified: yangkai.shen
*/
public interface QQ {

/**
* 获取QQ用户信息
* @return QQ用户信息
*/
QQUserInfo getUserInfo();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.xkcoding.social.qq.api;

import lombok.Data;

/**
* <p>
* QQ用户信息
* </p>
*
* @package: com.xkcoding.social.qq.api
* @description: QQ用户信息
* @author: yangkai.shen
* @date: Created in 2019-02-21 10:52
* @copyright: Copyright (c) 2019
* @version: V1.0
* @modified: yangkai.shen
*/
@Data
public class QQUserInfo {
/**
* QQ唯一标识
*/
private String openId;
/**
* 返回码
*/
private String ret;
/**
* 如果ret<0,会有相应的错误信息提示,返回数据全部用UTF-8编码。
*/
private String msg;
/**
* 用户在QQ空间的昵称。
*/
private String nickname;
/**
* 大小为30×30像素的QQ空间头像URL。
*/
private String figureurl;
/**
* 大小为50×50像素的QQ空间头像URL。
*/
private String figureurl_1;
/**
* 大小为100×100像素的QQ空间头像URL。
*/
private String figureurl_2;
/**
* 大小为40×40像素的QQ头像URL。
*/
private String figureurl_qq_1;
/**
* 大小为100×100像素的QQ头像URL。需要注意,不是所有的用户都拥有QQ的100x100的头像,但40x40像素则是一定会有。
*/
private String figureurl_qq_2;
/**
* 性别。 如果获取不到则默认返回"男"
*/
private String gender;
/**
* 是否黄钻
*/
private String is_yellow_vip;
/**
* 是否会员
*/
private String vip;
/**
* 黄钻等级
*/
private String yellow_vip_level;
/**
* 会员等级
*/
private String level;
/**
* 是否年费黄钻
*/
private String is_yellow_year_vip;


}
Loading

0 comments on commit b7be5c8

Please sign in to comment.