91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringBoot2 MyBatis如何集成CAS單點登錄

發布時間:2024-11-05 17:15:26 來源:億速云 閱讀:80 作者:小樊 欄目:編程語言

要在Spring Boot 2和MyBatis中集成CAS單點登錄,你需要遵循以下步驟:

  1. 添加依賴

在你的pom.xml文件中添加Java CAS客戶端庫和Spring Security依賴:

<dependency>
    <groupId>org.jasig.cas.client</groupId>
    <artifactId>cas-client-core</artifactId>
    <version>3.6.3</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
  1. 配置CAS客戶端

創建一個名為CasConfig.java的新類,并添加以下代碼:

import org.jasig.cas.client.cas20.Cas20ServiceTicketValidator;
import org.jasig.cas.client.config.Cas20Configuration;
import org.jasig.cas.client.config.Cas20ServiceRegistry;
import org.jasig.cas.client.config.SingleSignOutConfiguration;
import org.jasig.cas.client.util.CommonUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class CasConfig {

    @Value("${cas.server.prefix}")
    private String casServerPrefix;

    @Bean
    public Cas20ServiceTicketValidator cas20ServiceTicketValidator() {
        return new Cas20ServiceTicketValidator(casServerPrefix);
    }

    @Bean
    public Cas20Configuration cas20Configuration() {
        Cas20Configuration configuration = new Cas20Configuration();
        configuration.setServiceRegistry(casServiceRegistry());
        configuration.setSingleSignOutEnabled(true);
        configuration.setSignOutUrl(casServerPrefix + "/cas/logout");
        return configuration;
    }

    @Bean
    public Cas20ServiceRegistry casServiceRegistry() {
        Map<String, String> serviceRegistry = new HashMap<>();
        serviceRegistry.put("your-service-id", "http://localhost:8080/your-service-path");
        return new Cas20ServiceRegistry(serviceRegistry);
    }

    @Bean
    public SingleSignOutConfiguration singleSignOutConfiguration() {
        SingleSignOutConfiguration configuration = new SingleSignOutConfiguration();
        configuration.setCasServerLoginUrl(casServerPrefix + "/cas/login");
        return configuration;
    }
}

請根據你的CAS服務器信息替換casServerPrefixyour-service-id

  1. 配置Spring Security

創建一個名為SecurityConfig.java的新類,并添加以下代碼:

import org.jasig.cas.client.authentication.AttributePrincipal;
import org.springframework.beans.factory.annotation.Autowired;
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.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Autowired
    private UserDetailsService userDetailsService;

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

    @Bean
    public SecurityContextLogoutHandler securityContextLogoutHandler() {
        return new SecurityContextLogoutHandler();
    }

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

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

    @Bean
    public Cas20AuthenticationProvider cas20AuthenticationProvider() {
        Cas20AuthenticationProvider provider = new Cas20AuthenticationProvider();
        provider.setTicketValidator(cas20ServiceTicketValidator());
        provider.setServiceRegistry(casServiceRegistry());
        provider.setAuthenticationStrategy(new Cas20AuthenticationStrategy(cas20ServiceTicketValidator(), cas20Configuration(), casServiceRegistry()));
        return provider;
    }
}
  1. 配置MyBatis

在你的application.properties文件中添加以下配置,以便將CAS認證與MyBatis集成:

mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.yourproject.domain
spring.datasource.url=jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC
spring.datasource.username=your_username
spring.datasource.password=your_password
  1. 創建登錄和注銷控制器

創建一個名為LoginController.java的新類,并添加以下代碼:

import org.jasig.cas.client.authentication.AttributePrincipal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class LoginController {

    @Autowired
    private UserDetailsService userDetailsService;

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @PostMapping("/login")
    public String loginPost(AttributePrincipal principal) {
        String username = principal.getName();
        // 在這里驗證用戶憑據并設置UserDetails對象
        return "redirect:/home";
    }

    @GetMapping("/logout")
    public String logout() {
        return "redirect:/login?logout";
    }
}
  1. 創建主頁控制器

創建一個名為HomeController.java的新類,并添加以下代碼:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {

    @GetMapping("/home")
    public String home() {
        return "home";
    }
}

現在,你已經成功地將Spring Boot 2、MyBatis和CAS單點登錄集成在一起。用戶可以通過訪問/login URL進行登錄,然后訪問受保護的資源,如/home。用戶還可以通過訪問/logout URL進行注銷。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

依兰县| 东阿县| 普洱| 翼城县| 金华市| 新乡县| 慈溪市| 邳州市| 同德县| 分宜县| 大埔区| 郧西县| 且末县| 九江市| 宁南县| 宁安市| 临邑县| 揭阳市| 城固县| 高平市| 松原市| 阜南县| 卢氏县| 石家庄市| 威海市| 霞浦县| 蓬莱市| 秭归县| 克东县| 吴桥县| 张北县| 贵定县| 含山县| 甘孜| 南宁市| 清苑县| 金川县| 顺昌县| 新平| 锡林浩特市| 怀宁县|