উদাহরণ সহ Passay এবং Spring Security Integration

Passay এর Integration - প্যাসে (Passay) - Java Technologies

270

Passay এবং Spring Security ইন্টিগ্রেশন করা একটি শক্তিশালী পাসওয়ার্ড নিরাপত্তা ব্যবস্থা তৈরি করতে সাহায্য করে। Passay-এর পাসওয়ার্ড পলিসি ব্যবস্থাপনা এবং Spring Security-এর অ্যাক্সেস কন্ট্রোল এবং নিরাপত্তা বৈশিষ্ট্যগুলি একত্রে ব্যবহার করা হয়। নিচে Passay এবং Spring Security এর ইন্টিগ্রেশন সম্পর্কিত একটি উদাহরণ দেওয়া হলো।


Passay এবং Spring Security Integration উদাহরণ

এখানে একটি উদাহরণ দেখানো হচ্ছে, যেখানে Spring Security ব্যবহার করে একটি সিকিউরড অ্যাপ্লিকেশন তৈরি করা হবে এবং Passay ব্যবহার করা হবে পাসওয়ার্ডের নিয়ম যাচাই করার জন্য।


Step 1: Maven Dependencies

প্রথমে আপনাকে Passay এবং Spring Security এর জন্য Maven dependencies আপনার pom.xml ফাইলে যোগ করতে হবে।

<dependencies>
    <!-- Passay dependency -->
    <dependency>
        <groupId>org.passay</groupId>
        <artifactId>passay</artifactId>
        <version>1.6.0</version>
    </dependency>

    <!-- Spring Security dependency -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>5.8.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>5.8.0</version>
    </dependency>
</dependencies>

Step 2: Passay Password Validation Service

এখানে Passay ব্যবহার করে পাসওয়ার্ড যাচাই করার একটি সার্ভিস তৈরি করা হবে, যা Spring Security এর ইউজার রেজিস্ট্রেশন অথবা পাসওয়ার্ড চেঞ্জ সময়ে কাজ করবে।

import org.passay.*;
import org.springframework.stereotype.Service;

import java.util.Arrays;

@Service
public class PasswordValidationService {

    private final PasswordValidator validator;

    public PasswordValidationService() {
        this.validator = new PasswordValidator(Arrays.asList(
            new LengthRule(8, 16),               // দৈর্ঘ্য 8 থেকে 16 অক্ষর
            new UppercaseCharacterRule(1),       // অন্তত 1 বড় হাতের অক্ষর
            new DigitCharacterRule(1),           // অন্তত 1 সংখ্যা
            new SpecialCharacterRule(1),         // অন্তত 1 বিশেষ অক্ষর
            new WhitespaceRule()                 // ফাঁকা স্থান নিষিদ্ধ
        ));
    }

    public boolean validatePassword(String password) {
        RuleResult result = validator.validate(new PasswordData(password));
        return result.isValid();
    }

    public String getValidationMessages(String password) {
        RuleResult result = validator.validate(new PasswordData(password));
        return String.join(", ", validator.getMessages(result));
    }
}

এই PasswordValidationService ক্লাসে Passay এর PasswordValidator ব্যবহার করা হয়েছে পাসওয়ার্ড যাচাই করতে এবং কোন ত্রুটি বার্তা থাকলে তা ফিরিয়ে দিতে।


Step 3: Spring Security Configuration

এখন Spring Security কনফিগারেশন তৈরি করবো। এখানে একটি কাস্টম AuthenticationManager ব্যবহৃত হবে, যাতে পাসওয়ার্ড যাচাই করার জন্য Passay সার্ভিসটি ব্যবহার করা হয়।

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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 {

    private final PasswordValidationService passwordValidationService;

    public SecurityConfig(PasswordValidationService passwordValidationService) {
        this.passwordValidationService = passwordValidationService;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/register", "/login").permitAll() // রেজিস্ট্রেশন ও লগইন পেজের জন্য
                .anyRequest().authenticated() // অন্যান্য রিকোয়েস্টের জন্য প্রমাণীকরণ প্রয়োজন
            .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
            .and()
            .logout()
                .permitAll();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProvider());
    }

    @Bean
    public AuthenticationProvider authenticationProvider() {
        return new CustomAuthenticationProvider(passwordValidationService);
    }
}

Step 4: Custom Authentication Provider

এখন, একটি কাস্টম AuthenticationProvider তৈরি করা হবে যা পাসওয়ার্ড যাচাই করবে Passay ব্যবহার করে।

import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;

@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

    private final PasswordValidationService passwordValidationService;

    public CustomAuthenticationProvider(PasswordValidationService passwordValidationService) {
        this.passwordValidationService = passwordValidationService;
    }

    @Override
    public Authentication authenticate(Authentication authentication) {
        String username = authentication.getName();
        String password = authentication.getCredentials().toString();

        // Passay দিয়ে পাসওয়ার্ড যাচাই করা
        if (!passwordValidationService.validatePassword(password)) {
            throw new BadCredentialsException("পাসওয়ার্ড শর্ত পূরণ করে না।");
        }

        // ইউজার প্রমাণীকৃত হলে, একটি Authentication টোকেন তৈরি করা
        return new UsernamePasswordAuthenticationToken(
                new User(username, password, AuthorityUtils.NO_AUTHORITIES),
                password, AuthorityUtils.NO_AUTHORITIES);
    }

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

Step 5: Controller Example

এখানে একটি উদাহরণ কন্ট্রোলার দেখানো হচ্ছে যেখানে ইউজার রেজিস্ট্রেশন এবং পাসওয়ার্ড যাচাই করা হবে।

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class UserController {

    private final PasswordValidationService passwordValidationService;

    @Autowired
    public UserController(PasswordValidationService passwordValidationService) {
        this.passwordValidationService = passwordValidationService;
    }

    @PostMapping("/register")
    public String registerUser(@RequestParam String username, @RequestParam String password, Model model) {
        // পাসওয়ার্ড যাচাই করা
        if (passwordValidationService.validatePassword(password)) {
            model.addAttribute("message", "রেজিস্ট্রেশন সফল!");
            return "success";
        } else {
            model.addAttribute("error", passwordValidationService.getValidationMessages(password));
            return "register";
        }
    }
}

Step 6: Application Properties (Optional)

Spring Security এর জন্য কনফিগারেশন করতে আপনার application.properties বা application.yml ফাইল ব্যবহার করতে পারেন।


Conclusion:

এই উদাহরণে, Passay এবং Spring Security একত্রে ব্যবহার করা হয়েছে, যেখানে Passay পাসওয়ার্ড যাচাই করার জন্য কাস্টম রুল এবং পলিসি ব্যবহার করছে এবং Spring Security প্রমাণীকরণ এবং নিরাপত্তা নিশ্চিত করছে। এইভাবে আপনি একটি শক্তিশালী পাসওয়ার্ড নিরাপত্তা ব্যবস্থা তৈরি করতে পারবেন যা আপনার অ্যাপ্লিকেশনে উচ্চ সিকিউরিটি প্রদান করবে।

Content added By
Promotion

Are you sure to start over?

Loading...