legongju.com
我们一直在努力
2025-01-08 23:47 | 星期三

如何利用spring security实现单点登录

利用Spring Security实现单点登录(Single Sign-On, SSO)是一个常见的需求,特别是在企业级应用中。以下是一个基本的步骤指南,帮助你使用Spring Security实现SSO。

1. 引入依赖

首先,确保你的项目中已经引入了Spring Security相关的依赖。如果你使用的是Maven,可以在pom.xml中添加以下依赖:


    org.springframework.boot
    spring-boot-starter-security

2. 配置Spring Security

接下来,你需要配置Spring Security来启用认证和授权。你可以创建一个配置类来设置这些参数。

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.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/public/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

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

3. 创建登录页面

创建一个简单的登录页面login.html




    Login


    

Login

4. 实现SSO逻辑

为了实现SSO,你可以使用一个共享的认证中心(Authentication Center),例如使用Spring Security的UsernamePasswordAuthenticationFilterUsernamePasswordAuthenticationProvider

4.1 创建认证中心

创建一个配置类来设置认证中心:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
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;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
public class AuthenticationCenterConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/login").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

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

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("user").password(passwordEncoder().encode("password")).roles("USER");
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}

4.2 创建认证过滤器

创建一个自定义的认证过滤器来处理SSO逻辑:

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFilter {

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws Exception {
        if (!request.getMethod().equals("POST")) {
            throw new UnsupportedOperationException();
        }

        String username = obtainUsername(request);
        String password = obtainPassword(request);

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

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

        username = username.trim();

        UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);

        setDetails(request, authRequest);

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

    @Override
    protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws Exception {
        SecurityContextHolder.getContext().setAuthentication(authResult);
        response.sendRedirect(request.getContextPath() + "/home");
    }
}

4.3 配置自定义过滤器

SecurityConfig类中配置自定义过滤器:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/login").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

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

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("user").password(passwordEncoder().encode("password")).roles("USER");
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public CustomAuthenticationFilter customAuthenticationFilter() {
        CustomAuthenticationFilter filter = new CustomAuthenticationFilter();
        filter.setAuthenticationManager(authenticationManagerBean());
        return filter;
    }
}

5. 测试SSO

现在,你可以启动你的应用并测试SSO是否正常工作。首先访问/login页面,输入用户名和密码(在这个例子中是userpassword),然后你应该会被重定向到/home页面,这表明SSO已经成功实现。

总结

以上步骤展示了如何使用Spring Security实现单点登录。你可以根据需要扩展这个示例,例如添加更多的认证方式(如OAuth2、JWT等),或者集成其他认证中心(如LDAP、Active Directory等)。

未经允许不得转载 » 本文链接:https://www.legongju.com/article/66600.html

相关推荐

  • stomp在spring框架中的应用有哪些

    stomp在spring框架中的应用有哪些

    STOMP(Simple Text Oriented Messaging Protocol)是一种简单的文本定向消息传输协议,它可以在Web应用程序中实现实时通信。在Spring框架中,STOMP主要应用于We...

  • spring quartz怎样提高任务执行效率

    spring quartz怎样提高任务执行效率

    提高Spring Quartz任务执行效率可以从以下几个方面进行优化: 优化任务执行频率:根据业务需求,合理设置任务的执行频率。避免过于频繁的执行,以免消耗过多的系...

  • spring quartz在Spring Boot中如何用

    spring quartz在Spring Boot中如何用

    在Spring Boot中使用Spring Quartz,你需要遵循以下步骤: 添加依赖:在你的pom.xml文件中添加Spring Boot和Quartz的依赖。例如: org.springframework.boot spr...

  • spring quartz能处理复杂调度吗

    spring quartz能处理复杂调度吗

    Spring Quartz可以处理复杂的调度需求。Quartz是一个功能强大的开源作业调度框架,它支持复杂的调度需求,包括 Cron触发器、简单触发器、日历触发器等。此外,Qu...

  • spring security如何保护敏感数据

    spring security如何保护敏感数据

    Spring Security是一个强大的安全框架,它提供了多种机制来保护敏感数据,包括加密、访问控制、日志审计等。以下是Spring Security保护敏感数据的主要方法:

  • 在C语言中如何避免printf格式错误

    在C语言中如何避免printf格式错误

    在C语言中,使用printf函数时,如果格式字符串与提供的参数不匹配,可能会导致运行时错误,这种错误通常被称为“格式化错误”。为了避免这类错误,可以采取以下几...

  • printf函数有哪些参数需要注意

    printf函数有哪些参数需要注意

    printf函数是C语言中用于格式化输出的函数,其参数需要注意以下几点: 格式字符串:printf函数的第一个参数是格式字符串,它指定了输出格式。格式字符串中可以使...

  • C语言printf的输出缓冲机制是怎样的

    C语言printf的输出缓冲机制是怎样的

    C语言中的printf函数使用输出缓冲机制来提高输出效率。输出缓冲区是一个临时存储区域,用于暂存待输出的数据。当缓冲区满时,数据会被自动刷新到目标设备(如屏幕...