OAuth2 হল একটি জনপ্রিয় authorization framework, যা তৃতীয় পক্ষের অ্যাপ্লিকেশনকে সীমিত অ্যাক্সেস প্রদান করে ব্যবহারকারীর শংসাপত্র (credentials) শেয়ার না করে। Spring Security একটি শক্তিশালী ফ্রেমওয়ার্ক যা OAuth2 প্রোটোকল সহজেই ইন্টিগ্রেট করার সুবিধা দেয়।
Spring Security-তে OAuth2 ইন্টিগ্রেশন দুটি প্রধান ব্যবহারের ক্ষেত্রে প্রযোজ্য:
- OAuth2 Login:
- একটি তৃতীয় পক্ষের অ্যাপ্লিকেশন (যেমন, Google, Facebook) এর মাধ্যমে লগইন।
- সাধারণত Social Login বা Single Sign-On (SSO) এর জন্য ব্যবহৃত হয়।
- OAuth2 Resource Server:
- OAuth2 টোকেন যাচাই করার জন্য API তৈরি করা।
- একটি Bearer Token ব্যবহার করে সুরক্ষিত API অ্যাক্সেস।
১. OAuth2 Login Configuration
OAuth2 Login ব্যবহার করার ধাপ
১. Spring Security Dependency যুক্ত করুন
pom.xml ফাইলে OAuth2 এর জন্য প্রয়োজনীয় dependency যোগ করুন:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
২. OAuth2 Provider Configuration
application.yml বা application.properties ফাইলে OAuth2 provider-এর client details কনফিগার করুন। উদাহরণস্বরূপ, Google এর জন্য:
spring:
security:
oauth2:
client:
registration:
google:
client-id: YOUR_GOOGLE_CLIENT_ID
client-secret: YOUR_GOOGLE_CLIENT_SECRET
scope: profile, email
redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
client-name: Google
provider:
google:
authorization-uri: https://accounts.google.com/o/oauth2/auth
token-uri: https://oauth2.googleapis.com/token
user-info-uri: https://www.googleapis.com/oauth2/v3/userinfo
৩. Spring Security Configuration
Spring Security Configuration ক্লাস তৈরি করুন:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.oauth2Login(); // Enable OAuth2 Login
return http.build();
}
}
৪. Custom OAuth2 User Information
OAuth2 provider থেকে অতিরিক্ত তথ্য সংগ্রহ করতে OAuth2UserService কাস্টমাইজ করুন:
import org.springframework.security.oauth2.core.user.DefaultOAuth2User;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest;
import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserService;
import org.springframework.stereotype.Service;
import java.util.Collections;
@Service
public class CustomOAuth2UserService implements OAuth2UserService<OAuth2UserRequest, OAuth2User> {
@Override
public OAuth2User loadUser(OAuth2UserRequest userRequest) {
DefaultOAuth2User oauthUser = (DefaultOAuth2User) new DefaultOAuth2UserService().loadUser(userRequest);
// Extract custom user attributes if needed
String email = oauthUser.getAttribute("email");
return new DefaultOAuth2User(
Collections.singleton(() -> "ROLE_USER"),
oauthUser.getAttributes(),
"name"); // Map user identifier to "name"
}
}
২. OAuth2 Resource Server Configuration
Spring Security-তে Resource Server তৈরি করতে টোকেন যাচাই এবং API সুরক্ষিত করতে নিম্নলিখিত ধাপগুলো অনুসরণ করুন:
১. Spring Security Dependency যুক্ত করুন
pom.xml এ Resource Server এর জন্য dependency যোগ করুন:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
২. Resource Server Configuration
Resource Server সেটআপ করুন:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
public class ResourceServerConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/public").permitAll()
.anyRequest().authenticated()
.and()
.oauth2ResourceServer()
.jwt(); // Use JWT for token validation
return http.build();
}
@Bean
public JwtAuthenticationConverter jwtAuthenticationConverter() {
JwtAuthenticationConverter converter = new JwtAuthenticationConverter();
// Custom role mapping if necessary
return converter;
}
}
৩. Token Verification Configuration
application.yml বা application.properties এ token verification কনফিগার করুন:
spring:
security:
oauth2:
resourceserver:
jwt:
issuer-uri: https://accounts.google.com
৩. OAuth2 Client and Resource Server একত্রে ব্যবহার
একই অ্যাপ্লিকেশনে OAuth2 Login এবং Resource Server একসঙ্গে ব্যবহার করতে পারেন:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/public").permitAll()
.anyRequest().authenticated()
.and()
.oauth2Login()
.and()
.oauth2ResourceServer()
.jwt();
return http.build();
}
}
OAuth2 Integration-এর Best Practices
- Use HTTPS: সর্বদা OAuth2 এর জন্য HTTPS ব্যবহার করুন।
- Scope Limitation: শুধুমাত্র প্রয়োজনীয় scopes ব্যবহার করুন (যেমন
email,profile)। - Store Secrets Securely:
client-secretগুলো নিরাপদে সংরক্ষণ করুন। - Token Expiry and Refresh: টোকেন মেয়াদ শেষ হলে
refresh tokenব্যবহার করে পুনরায় টোকেন সংগ্রহ করুন। - Handle Token Revocation: ব্যবহারকারী যদি লগআউট করে তবে টোকেন নিষ্ক্রিয় করার জন্য ব্যবস্থা নিন।
উপসংহার
Spring Security-তে OAuth2 Integration ব্যবহার করে আপনি শক্তিশালী এবং সুরক্ষিত অ্যাপ্লিকেশন তৈরি করতে পারেন। Social Login, Single Sign-On (SSO), এবং Resource Server-এর মাধ্যমে OAuth2 সহজেই প্রয়োগ করা যায়। উল্লিখিত ধাপগুলি অনুসরণ করে আপনার OAuth2 ব্যবহারকারীর অভিজ্ঞতা উন্নত করতে পারবেন।
OAuth2 (Open Authorization 2.0) একটি জনপ্রিয় অথেন্টিকেশন এবং অথরাইজেশন ফ্রেমওয়ার্ক যা ব্যবহারকারীদের নিরাপদে তাদের রিসোর্স অ্যাক্সেস করার জন্য একটি স্ট্যান্ডার্ড প্রোটোকল সরবরাহ করে। এটি মূলত অনুমতি দেয় এমন একটি সিস্টেম যেখানে ব্যবহারকারী বা ক্লায়েন্ট অ্যাপ্লিকেশন তাদের রিসোর্স (যেমন API, ডাটা) অ্যাক্সেস করার জন্য অন্য একটি সিস্টেম (যেমন, গুগল, ফেসবুক) থেকে টোকেন লাভ করে।
স্প্রিং সিকিউরিটি (Spring Security) হল একটি শক্তিশালী নিরাপত্তা ফ্রেমওয়ার্ক যা স্প্রিং অ্যাপ্লিকেশনগুলোর জন্য নিরাপত্তা প্রদান করে এবং এটি সহজে OAuth2 প্রটোকলের মাধ্যমে অথেন্টিকেশন এবং অথরাইজেশন পরিচালনা করতে সক্ষম।
OAuth2 এর কাজের প্রক্রিয়া:
OAuth2 প্রোটোকল সাধারণত ৪টি প্রধান অংশে বিভক্ত:
- Resource Owner (সম্পদ মালিক): এটি সাধারণত ব্যবহারকারী (user) হয়, যিনি রিসোর্স বা ডেটার মালিক। তারা একটি নির্দিষ্ট অ্যাপ্লিকেশন বা সেবায় এক্সেস প্রদান করেন।
- Client (ক্লায়েন্ট): এটি সেই অ্যাপ্লিকেশন যা ব্যবহারকারী থেকে অনুমতি নিয়ে রিসোর্স অ্যাক্সেস করতে চায়। উদাহরণস্বরূপ, কোনো থার্ড-পার্টি অ্যাপ্লিকেশন।
- Authorization Server (অনুমতি প্রদানকারী সার্ভার): এটি সেই সার্ভার যা ব্যবহারকারীর পরিচয় যাচাই করে এবং ক্লায়েন্টকে অনুমতি প্রদান করে। এটি সাধারণত OAuth2 টোকেন (access token) প্রদান করে।
- Resource Server (রিসোর্স সার্ভার): এটি সেই সার্ভার যা রিসোর্স (যেমন, ব্যবহারকারীর প্রোফাইল তথ্য) সরবরাহ করে। রিসোর্স সার্ভার ব্যবহারকারীর অনুমতি যাচাই করার জন্য অ্যাক্সেস টোকেনের মাধ্যমে ক্লায়েন্টের অ্যাক্সেস কন্ট্রোল করে।
OAuth2 এর প্রধান কাজের প্রক্রিয়া:
- প্রথমে ক্লায়েন্ট অ্যাপ্লিকেশন (client application) ব্যবহারকারীর অনুমতি চায়। এটি সাধারণত ব্যবহারকারীকে একটি লগইন পেজ বা অথেন্টিকেশন পেজে পাঠায় (উদাহরণস্বরূপ, গুগল লগইন পেজ)।
- ব্যবহারকারী (resource owner) তার পরিচয় প্রদান করে এবং অনুমতি দেয় যাতে ক্লায়েন্ট অ্যাপ্লিকেশন তার নির্দিষ্ট রিসোর্স অ্যাক্সেস করতে পারে।
- একবার অনুমতি দেওয়ার পর, Authorization Server (অনুমতি প্রদানকারী সার্ভার) ক্লায়েন্ট অ্যাপ্লিকেশনকে একটি authorization code প্রদান করে।
- ক্লায়েন্ট অ্যাপ্লিকেশন তারপর এই authorization code কে token (অ্যাক্সেস টোকেন) এ রূপান্তরিত করতে Authorization Server এর কাছে পাঠায়।
- একবার ক্লায়েন্ট অ্যাপ্লিকেশন access token পেলে, এটি Resource Server (রিসোর্স সার্ভার)-এর কাছে রিসোর্স অ্যাক্সেসের জন্য টোকেন পাঠায়।
- Resource Server এই অ্যাক্সেস টোকেন যাচাই করে এবং এটি বৈধ হলে ক্লায়েন্টকে রিসোর্স (ডেটা) প্রদান করে।
OAuth2 এর প্রধান ধাপগুলো:
- Authorization Request – ক্লায়েন্ট অ্যাপ্লিকেশন ব্যবহারকারীকে অনুমতি চায়।
- Authorization Grant – ব্যবহারকারী অনুমতি দেয় এবং authorization code প্রদান করা হয়।
- Token Request – ক্লায়েন্ট authorization code দিয়ে access token অর্জন করে।
- Token Response – Authorization Server ক্লায়েন্টকে access token প্রদান করে।
- Access Resource – ক্লায়েন্ট access token দিয়ে Resource Server থেকে রিসোর্স অ্যাক্সেস করে।
স্প্রিং সিকিউরিটি এবং OAuth2:
স্প্রিং সিকিউরিটি OAuth2 প্রোটোকলটি সহজেই ইন্টিগ্রেট করার জন্য অনেক টুল এবং ফিচার সরবরাহ করে। স্প্রিং সিকিউরিটি OAuth2 এর সাহায্যে আপনি বিভিন্ন অথেন্টিকেশন সিস্টেম যেমন Facebook, Google, GitHub ইত্যাদি সহ ব্যবহারকারী অথেন্টিকেশন এবং অথরাইজেশন পরিচালনা করতে পারেন।
স্প্রিং সিকিউরিটি OAuth2 কনফিগার করার মাধ্যমে আপনার অ্যাপ্লিকেশনগুলোতে সিকিউরড অথেন্টিকেশন প্রক্রিয়া সহজে ইমপ্লিমেন্ট করা সম্ভব।
OAuth2 Authentication একটি জনপ্রিয় অথেনটিকেশন প্রোটোকল যা ব্যবহারকারীদের নিরাপদে এবং পারমিশন প্রাপ্ত অ্যাক্সেস প্রদান করতে সহায়ক। Spring Security OAuth2 প্রটোকলটি সমর্থন করে এবং একাধিক সোশ্যাল লগইন এবং ক্লাউড ভিত্তিক অথেনটিকেশন প্রদান করার জন্য এটি একটি খুবই কার্যকরী ফ্রেমওয়ার্ক।
Spring Security তে OAuth2 Authentication কনফিগার করার জন্য, সাধারণত OAuth2 Authorization Server এবং OAuth2 Resource Server কনফিগার করতে হয়।
OAuth2 Authentication কনফিগার করার ধাপগুলো
ধাপ ১: ডিপেন্ডেন্সি যোগ করুন
pom.xml ফাইলে নিচের ডিপেন্ডেন্সি যোগ করুন:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
এই ডিপেন্ডেন্সিগুলি OAuth2 ক্লায়েন্ট এবং রিসোর্স সার্ভার কনফিগারেশন সহ OAuth2 সম্পর্কিত সকল কনফিগারেশন পরিচালনা করতে সহায়ক।
ধাপ ২: OAuth2 ক্লায়েন্ট কনফিগারেশন
Spring Security তে OAuth2 ক্লায়েন্ট কনফিগার করার জন্য application.yml বা application.properties ফাইলে প্রয়োজনীয় কনফিগারেশন করতে হবে।
application.yml কনফিগারেশন উদাহরণ:
spring:
security:
oauth2:
client:
registration:
google:
client-id: YOUR_GOOGLE_CLIENT_ID
client-secret: YOUR_GOOGLE_CLIENT_SECRET
scope:
- profile
- email
redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
authorization-grant-type: authorization_code
client-name: Google
provider:
google:
authorization-uri: https://accounts.google.com/o/oauth2/auth
token-uri: https://oauth2.googleapis.com/token
user-info-uri: https://www.googleapis.com/oauth2/v3/userinfo
user-name-attribute: id
application.properties কনফিগারেশন উদাহরণ:
spring.security.oauth2.client.registration.google.client-id=YOUR_GOOGLE_CLIENT_ID
spring.security.oauth2.client.registration.google.client-secret=YOUR_GOOGLE_CLIENT_SECRET
spring.security.oauth2.client.registration.google.scope=profile,email
spring.security.oauth2.client.registration.google.redirect-uri={baseUrl}/login/oauth2/code/{registrationId}
spring.security.oauth2.client.registration.google.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.google.client-name=Google
spring.security.oauth2.client.provider.google.authorization-uri=https://accounts.google.com/o/oauth2/auth
spring.security.oauth2.client.provider.google.token-uri=https://oauth2.googleapis.com/token
spring.security.oauth2.client.provider.google.user-info-uri=https://www.googleapis.com/oauth2/v3/userinfo
spring.security.oauth2.client.provider.google.user-name-attribute=id
এখানে, client-id এবং client-secret গুগল ডেভেলপার কনসোলে তৈরি করা OAuth2 ক্লায়েন্ট থেকে পাবেন। এছাড়াও, আপনি যে OAuth2 প্রদানকারী (যেমন, Google, Facebook, GitHub ইত্যাদি) ব্যবহার করবেন, তার জন্য প্রাসঙ্গিক URL গুলি ব্যবহার করতে হবে।
ধাপ ৩: Spring Security কনফিগারেশন
Spring Security তে OAuth2 কনফিগার করতে HttpSecurity ব্যবহার করে লগইন, অ্যাক্সেস কন্ট্রোল ইত্যাদি কনফিগার করা হয়। এখানে ক্লায়েন্ট অ্যাপ্লিকেশন কনফিগার করা হচ্ছে।
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.oauth2.client.oauth2login.OAuth2LoginAuthenticationFilter;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.OAuth2LoginAuthenticationFilter;
import org.springframework.security.oauth2.client.web.OAuth2LoginAuthenticationFilter;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home", "/public/**").permitAll()
.anyRequest().authenticated()
.and()
.oauth2Login() // OAuth2 login config
.loginPage("/login")
.authorizationEndpoint()
.baseUri("/oauth2/authorization")
.and()
.redirectionEndpoint()
.baseUri("/login/oauth2/code/*")
.and()
.tokenEndpoint()
.accessTokenResponseClient(this.accessTokenResponseClient())
.and()
.userInfoEndpoint()
.userService(this.oauth2UserService());
}
@Bean
public OAuth2LoginAuthenticationFilter oauth2LoginAuthenticationFilter(ClientRegistrationRepository clientRegistrationRepository) {
OAuth2LoginAuthenticationFilter oauth2LoginAuthenticationFilter = new OAuth2LoginAuthenticationFilter(
clientRegistrationRepository,
new DefaultOAuth2UserService()
);
oauth2LoginAuthenticationFilter.setAuthenticationManager(authenticationManager());
return oauth2LoginAuthenticationFilter;
}
}
এই কনফিগারেশনে:
oauth2Login()ব্যবহার করে OAuth2 লগইন সক্রিয় করা হয়েছে।/oauth2/authorizationএবং/login/oauth2/code/*এই দুইটি URL কনফিগার করা হয়েছে।OAuth2UserServiceব্যবহার করা হয়েছে, যা OAuth2 অথেনটিকেশন পরবর্তীতে ব্যবহারকারীর তথ্য ফেচ করতে সহায়ক।
ধাপ ৪: ইউজার তথ্য গ্রহণ করা
OAuth2 Authentication এর পর ব্যবহারকারীর তথ্য (যেমন নাম, ইমেইল) পাওয়ার জন্য OAuth2UserService কাস্টমাইজ করা যেতে পারে।
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.security.oauth2.core.user.DefaultOAuth2User;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import java.util.Collections;
public class CustomOAuth2UserService implements OAuth2UserService<OAuth2UserRequest, OAuth2User> {
@Override
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
OAuth2User oAuth2User = super.loadUser(userRequest);
// ব্যবহারকারীর তথ্য প্রাপ্তি এবং কাস্টমাইজেশন
String username = oAuth2User.getAttribute("name");
String email = oAuth2User.getAttribute("email");
// Return custom OAuth2 user
return new DefaultOAuth2User(
Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER")),
oAuth2User.getAttributes(),
"name"
);
}
}
ধাপ ৫: OAuth2 Resource Server কনফিগারেশন
আপনি যদি একটি OAuth2 Resource Server তৈরি করতে চান (যেখানে অ্যাক্সেস টোকেনের মাধ্যমে রিসোর্স অ্যাক্সেস করা হয়), তবে নিচের কনফিগারেশনটি ব্যবহার করুন:
@Configuration
@EnableWebSecurity
public class ResourceServerConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/secure/**").authenticated()
.anyRequest().permitAll()
.and()
.oauth2ResourceServer()
.jwt(); // OAuth2 JWT ব্যবহার
return http.build();
}
}
এখানে oauth2ResourceServer().jwt() ব্যবহার করা হয়েছে, যা JWT টোকেনের মাধ্যমে অথেনটিকেশন পরিচালনা করে।
উপসংহার
Spring Security তে OAuth2 Authentication কনফিগার করা একটি শক্তিশালী এবং নিরাপদ অথেনটিকেশন ব্যবস্থা তৈরি করতে সাহায্য করে, যা ক্লায়েন্ট অ্যাপ্লিকেশনগুলোর জন্য তৃতীয় পক্ষের সেবা (যেমন, Google, Facebook) এর মাধ্যমে লগইন সুবিধা প্রদান করে। এই প্রক্রিয়ার মাধ্যমে আপনি সহজেই OAuth2 ক্লায়েন্ট এবং রিসোর্স সার্ভার কনফিগার করতে পারেন।
OAuth2 (Open Authorization 2) হল একটি অটোরাইজেশন ফ্রেমওয়ার্ক যা ব্যবহারকারীদের তাদের তৃতীয় পক্ষের অ্যাকাউন্ট (যেমন Google, Facebook, GitHub, Twitter) দিয়ে আপনার অ্যাপ্লিকেশনে লগইন করতে সক্ষম করে। এই প্রক্রিয়ায়, ব্যবহারকারীরা তাদের প্রমাণীকরণ এবং অনুমোদন তৃতীয় পক্ষের প্রদানকারী (যেমন Google) এর মাধ্যমে সম্পন্ন করেন এবং তারপর আপনার অ্যাপ্লিকেশনকে নির্দিষ্ট ডেটা অ্যাক্সেস করতে অনুমতি দেন।
Spring Security আপনাকে সহজেই OAuth2.0 ব্যবহার করে তৃতীয় পক্ষের লগইন ইন্টিগ্রেট করতে সাহায্য করে। এতে সাধারণত Google বা GitHub এর মত OAuth2 সাপোর্ট করা প্রোভাইডার ব্যবহার করা হয়।
OAuth2 Third-Party Login Integration (Spring Security)
Spring Security OAuth2 অটোমেটিক্যালি OAuth2 প্রটোকলের মাধ্যমে তৃতীয় পক্ষের সেবা ব্যবহারকারীর অ্যাক্সেস করতে এবং প্রমাণীকরণ করতে সক্ষম করে। এখানে Google এর মাধ্যমে OAuth2 login ইন্টিগ্রেট করার উদাহরণ দেওয়া হলো।
ধাপ ১: ডিপেন্ডেন্সি যোগ করা
Spring Boot প্রজেক্টে OAuth2 সমর্থন যোগ করতে প্রথমে spring-boot-starter-oauth2-client ডিপেন্ডেন্সি যুক্ত করুন।
Maven:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
Gradle:
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
ধাপ ২: অ্যাপ্লিকেশন কনফিগারেশন
Spring Boot অ্যাপ্লিকেশনে OAuth2 কনফিগারেশনের জন্য, application.yml বা application.properties ফাইলে প্রোভাইডারের তথ্য যুক্ত করুন। এখানে উদাহরণ হিসেবে Google OAuth2 কনফিগারেশন দেওয়া হয়েছে।
application.yml ফাইল কনফিগারেশন:
spring:
security:
oauth2:
client:
registration:
google:
client-id: YOUR_GOOGLE_CLIENT_ID
client-secret: YOUR_GOOGLE_CLIENT_SECRET
scope:
- openid
- profile
- email
redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
authorization-grant-type: authorization_code
client-name: Google
provider:
google:
authorization-uri: https://accounts.google.com/o/oauth2/auth
token-uri: https://oauth2.googleapis.com/token
user-info-uri: https://www.googleapis.com/oauth2/v3/userinfo
এখানে, আপনাকে YOUR_GOOGLE_CLIENT_ID এবং YOUR_GOOGLE_CLIENT_SECRET আপনার Google Developer Console থেকে নিতে হবে।
ধাপ ৩: Spring Security কনফিগারেশন
Spring Security-এর কনফিগারেশনে OAuth2 লগইন সক্ষম করতে WebSecurityConfigurerAdapter কনফিগার করতে হবে।
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/login").permitAll() // Public pages
.anyRequest().authenticated() // Secure pages require authentication
.and()
.oauth2Login() // Enable OAuth2 Login
.loginPage("/login"); // Custom login page (optional)
}
}
এই কনফিগারেশনটি OAuth2 লোগিন পেজ তৈরি করে এবং Google OAuth2 মাধ্যমে লগইন করার সুযোগ দেয়।
ধাপ ৪: Custom Login Page (অপশনাল)
যদি আপনি কাস্টম লগইন পেজ ব্যবহার করতে চান, তবে আপনি একটি Controller এবং একটি থাইমলিফ (Thymeleaf) বা HTML পেজ তৈরি করতে পারেন।
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class LoginController {
@GetMapping("/login")
public String login() {
return "login"; // Login page view (HTML or Thymeleaf template)
}
}
login.html (উদাহরণ)
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login Page</h2>
<a href="/oauth2/authorization/google">Login with Google</a>
</body>
</html>
ধাপ ৫: প্রোফাইল ডেটা পাওয়া
OAuth2 লোগইন সফল হলে, ব্যবহারকারীর প্রোফাইল ডেটা পাবেন। এটি ব্যবহারের জন্য OAuth2User এবং OAuth2AuthenticationToken ব্যবহার করতে হবে।
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.security.oauth2.core.user.OAuth2UserAuthority;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
@Controller
public class ProfileController {
@GetMapping("/profile")
public String getProfile(@AuthenticationPrincipal OAuth2User principal, Model model) {
model.addAttribute("name", principal.getAttribute("name"));
model.addAttribute("email", principal.getAttribute("email"));
return "profile"; // Display user's profile
}
}
profile.html (উদাহরণ)
<!DOCTYPE html>
<html>
<head>
<title>User Profile</title>
</head>
<body>
<h2>Welcome, {{name}}</h2>
<p>Email: {{email}}</p>
</body>
</html>
ধাপ ৬: Testing OAuth2 Login
- আপনার অ্যাপ্লিকেশন চালু করুন।
/loginপৃষ্ঠায় গিয়ে "Login with Google" এ ক্লিক করুন।- Google এর লগইন স্ক্রীনে গিয়ে অনুমতি প্রদান করুন।
- সফল লগইন হলে, ব্যবহারকারীর প্রোফাইল ডেটা
/profileপৃষ্ঠায় প্রদর্শিত হবে।
উপসংহার
Spring Security দিয়ে OAuth2 তৃতীয় পক্ষের লগইন ইন্টিগ্রেট করা খুবই সহজ এবং সুরক্ষিত। এই প্রক্রিয়া দিয়ে আপনি বিভিন্ন প্রোভাইডার (যেমন Google, Facebook, GitHub, etc.) থেকে ব্যবহারকারীদের লগইন করতে সক্ষম হবেন। এতে, ব্যবহারকারীরা তাদের পরিচয় যাচাইয়ের জন্য তৃতীয় পক্ষের সেবার উপর নির্ভর করে, এবং আপনার অ্যাপ্লিকেশন তাদের কাছ থেকে শুধু প্রয়োজনীয় ডেটা নেয়।
OAuth2 (Open Authorization 2.0) হল একটি জনপ্রিয় অথেন্টিকেশন এবং অথরাইজেশন প্রোটোকল যা বিশেষ করে Third-party Authentication (যেমন Google, Facebook, GitHub) জন্য ব্যবহৃত হয়। Spring Security OAuth2 ব্যবহার করে আপনি খুব সহজে OAuth2 ভিত্তিক Authentication এবং Authorization কনফিগার করতে পারেন।
এই গাইডে, আমরা দেখব কিভাবে Spring Security এবং OAuth2 ইন্টিগ্রেশন করা যায় এবং ব্যবহারকারীরা কীভাবে Google এর মাধ্যমে লগইন করতে পারে।
১. Maven ডিপেনডেন্সি
আপনার pom.xml-এ Spring Security OAuth2 এবং Spring Boot Starter Web এর জন্য ডিপেনডেন্সি যোগ করতে হবে।
<dependencies>
<!-- Spring Security OAuth2 Client Dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<!-- Spring Boot Web Dependency for the Web app -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Starter Security for Security Configuration -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
২. application.yml বা application.properties কনফিগারেশন
OAuth2 ইন্টিগ্রেশন করার জন্য আপনাকে আপনার OAuth2 provider (যেমন Google, Facebook) থেকে Client ID এবং Client Secret নিতে হবে। এখানে আমরা Google OAuth2 উদাহরণ দেব।
application.yml কনফিগারেশন:
spring:
security:
oauth2:
client:
registration:
google:
client-id: YOUR_GOOGLE_CLIENT_ID
client-secret: YOUR_GOOGLE_CLIENT_SECRET
scope: profile, email
redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
authorization-grant-type: authorization_code
client-name: Google
provider:
google:
authorization-uri: https://accounts.google.com/o/oauth2/auth
token-uri: https://oauth2.googleapis.com/token
user-info-uri: https://www.googleapis.com/oauth2/v3/userinfo
user-name-attribute: id
client-idএবংclient-secretআপনি Google Developers Console থেকে পাবেন।scopeব্যবহারকারীর প্রোফাইল এবং ইমেইল এক্সেসের জন্য।redirect-uriহলো সেই URL যেখানে OAuth2 প্রক্রিয়া শেষে ব্যবহারকারীকে রিডাইরেক্ট করা হবে।
৩. Spring Security কনফিগারেশন
Spring Security কনফিগারেশনে OAuth2 authentication প্রক্রিয়া সক্রিয় করতে হবে। SecurityConfig.java ফাইলে নিচের কনফিগারেশন করা যেতে পারে:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.client.oidc.userinfo.OidcUserService;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.registration.OAuth2AuthorizationCodeGrantRequestEntityConverter;
import org.springframework.security.oauth2.client.registration.OAuth2LoginAuthenticationProvider;
import org.springframework.security.oauth2.client.registration.OAuth2LoginAuthenticationFilter;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login", "/error").permitAll() // লগইন পেজ এবং এরর পেজ সবার জন্য উন্মুক্ত
.anyRequest().authenticated() // অন্যান্য রিকোয়েস্টের জন্য অথেন্টিকেশন প্রয়োজন
.and()
.oauth2Login() // OAuth2 Login সক্রিয়
.loginPage("/login") // কাস্টম লগইন পেজ
.failureUrl("/error"); // লগইন ব্যর্থ হলে রিডাইরেক্ট করা হবে
return http.build();
}
}
- এখানে,
oauth2Login()মেথড OAuth2 লগইন কনফিগার করে। আপনিloginPage()ওfailureUrl()মেথড দিয়ে কাস্টম লোগিন এবং এরর পেজও নির্ধারণ করতে পারেন।
৪. Google OAuth2 লগইন ইউআরএল সেট করা
ব্যবহারকারীদের লগইন করতে একটি লিংক তৈরি করতে পারেন:
@Controller
public class LoginController {
@GetMapping("/login")
public String loginPage() {
return "login"; // Custom login page view
}
@GetMapping("/login-success")
public String loginSuccessPage() {
return "login-success"; // Success page after login
}
@GetMapping("/error")
public String errorPage() {
return "error"; // Error page if login fails
}
}
- এখানে,
/loginহল কাস্টম লগইন পেজ যেখানে ইউজারকে লগইন করতে বলা হবে। /login-successহল সাফল্যের পেজ যেখানে লগইন সফল হলে ইউজার যাবে।/errorপেজটি ত্রুটি ক্ষেত্রে ব্যবহার হবে।
৫. OAuth2 UserDetailsService কনফিগারেশন
Google OAuth2 প্রোভাইডার থেকে প্রাপ্ত ইউজার তথ্য পেতে OAuth2UserService ব্যবহার করা হবে:
import org.springframework.security.oauth2.client.userinfo.OAuth2UserService;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
@Service
public class CustomOAuth2UserService implements OAuth2UserService<OAuth2UserRequest, OAuth2User> {
@Override
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
OAuth2User oAuth2User = super.loadUser(userRequest);
// Customize the user object by extracting details from OAuth2User
String username = oAuth2User.getAttribute("name");
String email = oAuth2User.getAttribute("email");
return new User(username, email, Collections.emptyList());
}
}
এখানে OAuth2User অবজেক্ট থেকে ইউজারের তথ্য বের করে, যেমন name, email ইত্যাদি।
৬. HTML পেজ কনফিগারেশন
কাস্টম লোগিন পেজ তৈরি করতে পারেন যেখানে ইউজারকে Google Login বাটন দেখানো হবে।
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<a href="/oauth2/authorization/google">Login with Google</a>
</body>
</html>
এটি একটি সাধারণ HTML পেজ, যেখানে Google OAuth2 লগইন বাটন দেওয়া হয়েছে। ক্লিক করলে Google OAuth2 Login পেজে রিডাইরেক্ট করা হবে।
৭. লগইন সফল হলে ইউজারের তথ্য পাওয়া
ব্যবহারকারীর তথ্য পাওয়ার জন্য Spring Security OAuth2 ব্যবহার করতে পারেন।
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@GetMapping("/user")
public String userInfo() {
OAuth2User principal = (OAuth2User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
return "User Info: " + principal.getAttributes();
}
}
এটি লগইন করা ব্যবহারকারীর তথ্য (যেমন নাম, ইমেইল) প্রদর্শন করবে।
উপসংহার
Spring Security-এর সাহায্যে OAuth2 ইন্টিগ্রেশন একটি সহজ ও নিরাপদ প্রক্রিয়া যা আপনাকে বিভিন্ন OAuth2 প্রদানকারীর (যেমন Google, Facebook) মাধ্যমে ব্যবহারকারীকে লগইন করতে সহায়তা করে। এইভাবে আপনি আপনার অ্যাপ্লিকেশনে Single Sign-On (SSO) সমর্থন করতে পারেন।
Read more