Skip to content

Commit 16716b5

Browse files
committed
refactoring spring boot social
1 parent fd38c5b commit 16716b5

33 files changed

+315
-25
lines changed

.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,36 @@ SpringBootSocial/build/
7474
SpringBootSocial/libs/
7575
SpringBootValidator/build/
7676
SpringBootWebFlux/build/
77+
/SpringBootSocial/src/main/resources/application-social.properties
78+
SpringBootAsync/src/main/resources/static/
79+
SpringBootAsync/src/main/resources/templates/
80+
SpringBootCustomJackson/src/main/resources/static/
81+
SpringBootCustomJackson/src/main/resources/templates/
82+
SpringBootExcel/build/
83+
SpringBootException/src/main/resources/
84+
SpringBootException/src/test/
85+
SpringBootInterceptor/src/main/resources/static/
86+
SpringBootInterceptor/src/main/resources/templates/
87+
SpringBootOAuth2/src/main/java/com/example/config/
88+
SpringBootOAuth2/src/main/java/com/example/domain/
89+
SpringBootOAuth2/src/main/java/com/example/service/
90+
SpringBootOAuth2/src/main/resources/static/
91+
SpringBootOAuth2/src/main/resources/templates/
92+
SpringBootQueryDsl/src/main/resources/static/
93+
SpringBootQueryDsl/src/main/resources/templates/
94+
SpringBootRabbitMQ/src/main/resources/
95+
SpringBootRabbitMQ/src/test/
96+
SpringBootRest/src/main/resources/static/
97+
SpringBootRest/src/main/resources/templates/
98+
SpringBootRestDocs/src/main/resources/static/
99+
SpringBootRestDocs/src/main/resources/templates/
100+
SpringBootSecurityAddRedis/src/main/resources/static/
101+
SpringBootSecurityAddRedis/src/main/resources/templates/
102+
SpringBootSecurityAddRedis/src/test/
103+
SpringBootSecurityJwt/src/main/resources/static/
104+
SpringBootSecurityJwt/src/main/resources/templates/
105+
SpringBootSwagger/src/main/resources/static/
106+
SpringBootSwagger/src/main/resources/templates/
107+
SpringBootTest/src/main/resources/static/
108+
SpringBootTest/src/main/resources/templates/
109+
SpringBootWebSocket/build/

SpringBootSocial/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Custom Library
1+
##Custom Library
22

33
Spring-Social-Kakao : https://github.com/bongki/spring-social-kakao
44

SpringBootSocial/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ repositories {
3232

3333

3434
dependencies {
35+
runtime('com.h2database:h2')
3536
compile('org.projectlombok:lombok:1.16.6')
3637

3738
compile("org.webjars:bootstrap:3.3.7")
3839
compile("org.webjars:jquery:3.1.0")
3940

41+
compile('org.springframework.boot:spring-boot-starter-data-jpa')
42+
compile('org.springframework.boot:spring-boot-starter-security')
4043
compile("org.springframework.social:spring-social-security")
4144
compile('org.springframework.boot:spring-boot-starter-social-facebook')
4245
compile files("libs/spring-social-kakao-1.0.0.jar")
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.example.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
5+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
6+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
7+
import org.springframework.social.security.SpringSocialConfigurer;
8+
9+
@Configuration
10+
@EnableWebSecurity
11+
public class SecurityConfig extends WebSecurityConfigurerAdapter {
12+
@Override
13+
protected void configure(HttpSecurity http) throws Exception {
14+
15+
http
16+
.csrf().disable()
17+
.authorizeRequests()
18+
.anyRequest()
19+
.anonymous().and()
20+
.formLogin()
21+
.loginPage("/login")
22+
.defaultSuccessUrl("/", true)
23+
.loginProcessingUrl("/login/authenticate")
24+
.and()
25+
.logout()
26+
.deleteCookies("SESSION")
27+
.logoutUrl("/logout")
28+
.logoutSuccessUrl("/login")
29+
.and()
30+
.authorizeRequests()
31+
.antMatchers(
32+
"/auth/**",
33+
"/login",
34+
"/error",
35+
"/signup",
36+
"/css/**",
37+
"/js/**"
38+
).permitAll()
39+
.antMatchers("/**").hasRole("USER")
40+
.and()
41+
.apply(new SpringSocialConfigurer());
42+
}
43+
}
Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,74 @@
11
package com.example.config;
22

3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Bean;
35
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.core.env.Environment;
7+
import org.springframework.security.crypto.encrypt.Encryptors;
8+
import org.springframework.social.UserIdSource;
9+
import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
410
import org.springframework.social.config.annotation.EnableSocial;
11+
import org.springframework.social.config.annotation.SocialConfigurer;
12+
import org.springframework.social.connect.ConnectionFactoryLocator;
13+
import org.springframework.social.connect.ConnectionRepository;
14+
import org.springframework.social.connect.UsersConnectionRepository;
15+
import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository;
16+
import org.springframework.social.connect.web.ConnectController;
17+
import org.springframework.social.connect.web.ProviderSignInUtils;
18+
import org.springframework.social.facebook.connect.FacebookConnectionFactory;
19+
import org.springframework.social.kakao.connect.KakaoConnectionFactory;
20+
import org.springframework.social.naver.connect.NaverConnectionFactory;
21+
import org.springframework.social.security.AuthenticationNameUserIdSource;
22+
23+
import javax.sql.DataSource;
524

625
@Configuration
726
@EnableSocial
8-
public class SocialConfig {
27+
public class SocialConfig implements SocialConfigurer {
28+
29+
@Autowired DataSource dataSource;
30+
31+
@Override
32+
public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment environment) {
33+
// cfConfig.addConnectionFactory(facebookConnectionFactory(environment));
34+
cfConfig.addConnectionFactory(kakaoConnectionFactory(environment));
35+
cfConfig.addConnectionFactory(naverConnectionFactory(environment));
36+
}
37+
38+
@Override
39+
public UserIdSource getUserIdSource() {
40+
return new AuthenticationNameUserIdSource();
41+
}
42+
43+
@Override
44+
public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
45+
return new JdbcUsersConnectionRepository(dataSource, connectionFactoryLocator, Encryptors.noOpText());
46+
}
47+
48+
@Bean
49+
public ConnectController connectController(ConnectionFactoryLocator connectionFactoryLocator,
50+
ConnectionRepository connectionRepository) {
51+
return new ConnectController(connectionFactoryLocator, connectionRepository);
52+
}
53+
54+
@Bean
55+
public ProviderSignInUtils providerSignInUtils(ConnectionFactoryLocator connectionFactoryLocator,
56+
UsersConnectionRepository usersConnectionRepository) {
57+
return new ProviderSignInUtils(connectionFactoryLocator, usersConnectionRepository);
58+
}
59+
60+
private FacebookConnectionFactory facebookConnectionFactory(Environment environment) {
61+
return new FacebookConnectionFactory(environment.getProperty("spring.social.facebook.appId"),
62+
environment.getProperty("spring.social.facebook.appSecret"));
63+
}
64+
65+
private KakaoConnectionFactory kakaoConnectionFactory(Environment environment) {
66+
return new KakaoConnectionFactory(environment.getProperty("spring.social.kakao.appId"));
67+
}
68+
69+
70+
private NaverConnectionFactory naverConnectionFactory(Environment environment) {
71+
return new NaverConnectionFactory(environment.getProperty("spring.social.naver.appId"),
72+
environment.getProperty("spring.social.naver.appSecret"));
73+
}
974
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.example.domain;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
6+
import javax.persistence.Entity;
7+
import javax.persistence.Id;
8+
import java.io.Serializable;
9+
10+
@Data
11+
@Entity
12+
@AllArgsConstructor
13+
public class SimpleUser implements Serializable {
14+
15+
@Id
16+
private Long idx;
17+
18+
private String email;
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example.repository;
2+
3+
import com.example.domain.SimpleUser;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface SimpleUserRepository extends JpaRepository<SimpleUser, Long> {
9+
10+
SimpleUser findByEmail(String email);
11+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.example.security;
2+
3+
import com.example.domain.SimpleUser;
4+
import com.example.repository.SimpleUserRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.security.core.authority.AuthorityUtils;
7+
import org.springframework.security.core.userdetails.User;
8+
import org.springframework.security.core.userdetails.UserDetails;
9+
import org.springframework.security.core.userdetails.UserDetailsService;
10+
import org.springframework.security.core.userdetails.UsernameNotFoundException;
11+
import org.springframework.stereotype.Service;
12+
13+
@Service
14+
public class UserDetailsServiceImpl implements UserDetailsService {
15+
16+
@Autowired SimpleUserRepository simpleUserRepository;
17+
18+
@Override
19+
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
20+
SimpleUser user = simpleUserRepository.findByEmail(username);
21+
return new User(user.getEmail(), "", AuthorityUtils.createAuthorityList("ROLL_SOCIAL"));
22+
}
23+
}

SpringBootSocial/src/main/java/com/example/social/SocialUsersDetailService.java renamed to SpringBootSocial/src/main/java/com/example/social/SocialUserDetailsServiceImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.example.social;
22

33
import org.springframework.beans.factory.annotation.Autowired;
4-
import org.springframework.dao.DataAccessException;
54
import org.springframework.security.core.userdetails.UserDetails;
65
import org.springframework.security.core.userdetails.UserDetailsService;
76
import org.springframework.security.core.userdetails.UsernameNotFoundException;
@@ -10,12 +9,12 @@
109
import org.springframework.stereotype.Service;
1110

1211
@Service
13-
public class SocialUsersDetailService implements SocialUserDetailsService {
12+
public class SocialUserDetailsServiceImpl implements SocialUserDetailsService {
1413

1514
@Autowired UserDetailsService userDetailsService;
1615

1716
@Override
18-
public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException, DataAccessException {
17+
public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException {
1918
UserDetails userDetails = userDetailsService.loadUserByUsername(userId);
2019
return (SocialUserDetailsImpl) userDetails;
2120
}

SpringBootSocial/src/main/java/org/springframework/social/naver/api/Naver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package java.org.springframework.social.naver.api;
1+
package org.springframework.social.naver.api;
22

33
import org.springframework.social.ApiBinding;
44
import org.springframework.social.naver.api.abstracts.UserOperation;

0 commit comments

Comments
 (0)