CAS 5.3.1系列之自定義JDBC認證策略(三)

      網友投稿 1000 2025-04-01

      CAS 5.3.1系列之自定義JDBC認證策略(三)


      CAS官方文檔是介紹基于配置實現jdbc認證的,可以參考我博客:CAS 5.3.1系列之支持JDBC認證登錄(二),不過我們也可以通過自定義認證策略的方式實現jdbc認證,pom先加入相關jar

      org.apereo.cas cas-server-core-authentication-api ${cas.version} org.apereo.cas cas-server-core-configuration-api ${cas.version} org.apereo.cas cas-server-support-generic ${cas.version}

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      5.3.1版本可以實現AbstractPreAndPostProcessingAuthenticationHandler抽象類,重寫doAuthentication方法實現:

      package org.muses.jeeplatform.cas.authentication.handler; import org.apereo.cas.authentication.*; import org.apereo.cas.authentication.handler.support.AbstractPreAndPostProcessingAuthenticationHandler; import org.apereo.cas.authentication.principal.PrincipalFactory; import org.apereo.cas.services.ServicesManager; import org.muses.jeeplatform.cas.user.model.User; import org.slf4j.LoggerFactory; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; import javax.security.auth.login.AccountException; import javax.security.auth.login.FailedLoginException; import java.security.GeneralSecurityException; import java.util.*; public class UsernamePasswordAuthenticationHandler extends AbstractPreAndPostProcessingAuthenticationHandler { private static final org.slf4j.Logger logger = LoggerFactory.getLogger(UsernamePasswordAuthenticationHandler.class); public UsernamePasswordAuthenticationHandler(String name, ServicesManager servicesManager, PrincipalFactory principalFactory, Integer order) { super(name, servicesManager, principalFactory, order); } @Override protected AuthenticationHandlerExecutionResult doAuthentication(Credential credential) throws GeneralSecurityException, PreventedException { UsernamePasswordCredential usernamePasswordCredential = (UsernamePasswordCredential) credential; String username = usernamePasswordCredential.getUsername(); String password = usernamePasswordCredential.getPassword(); // 先構建數據庫驅動連接池 DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://192.169.0.198:33306/jeeplatform"); dataSource.setUsername("root"); dataSource.setPassword("root"); // 創建JDBC模板 JdbcTemplate jdbcTemplate = new JdbcTemplate(); jdbcTemplate.setDataSource(dataSource); String sql = "SELECT * FROM sys_user WHERE username = ?"; User info = (User) jdbcTemplate.queryForObject(sql, new Object[]{username}, new BeanPropertyRowMapper(User.class)); if (info == null) { throw new AccountException("Sorry, username not found!"); } if (!info.getPassword().equals(password)) { throw new FailedLoginException("Sorry, password not correct!"); } else { final List list = new ArrayList<>(); return createHandlerResult(usernamePasswordCredential, this.principalFactory.createPrincipal(username, Collections.emptyMap()), list); } } @Override public boolean supports(Credential credential) { return credential instanceof UsernamePasswordCredential; } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

      46

      47

      48

      49

      50

      51

      52

      53

      54

      55

      56

      57

      58

      59

      60

      61

      62

      63

      CAS 5.3.1系列之自定義JDBC認證策略(三)

      64

      65

      66

      67

      68

      69

      70

      71

      新增一個配置類,實現AuthenticationEventExecutionPlanConfigurer接口

      package org.muses.jeeplatform.cas.authentication.config; import org.apereo.cas.authentication.*; import org.apereo.cas.authentication.principal.DefaultPrincipalFactory; import org.apereo.cas.configuration.CasConfigurationProperties; import org.apereo.cas.services.ServicesManager; import org.muses.jeeplatform.cas.authentication.handler.UsernamePasswordAuthenticationHandler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration("UsernamePasswordAuthConfig") @EnableConfigurationProperties(CasConfigurationProperties.class) public class UsernamePasswordAuthConfig implements AuthenticationEventExecutionPlanConfigurer { @Autowired private CasConfigurationProperties casProperties; @Autowired @Qualifier("servicesManager") private ServicesManager servicesManager; @Bean public PrePostAuthenticationHandler myAuthenticationHandler() { return new UsernamePasswordAuthenticationHandler(UsernamePasswordAuthenticationHandler.class.getName(), servicesManager, new DefaultPrincipalFactory(), 1); } @Override public void configureAuthenticationExecutionPlan(AuthenticationEventExecutionPlan plan) { plan.registerAuthenticationHandler(myAuthenticationHandler()); } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      在META-INF文件夾,新增一個命名為spring.factories的文件

      org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.muses.jeeplatform.cas.authentication.config.UsernamePasswordAuthConfig

      1

      2

      3

      為什么要這樣做?因為這樣做才能將配置類加載到spring容器,詳情需要跟下源碼,可以參考我博客:SpringBoot源碼學習系列之自動配置原理簡介

      代碼例子參考:github下載鏈接

      詳情可以參考官方文檔:https://apereo.github.io/cas/5.3.x/installation/Configuration-Properties.html

      優質參考博客:

      https://www.cnblogs.com/jpeanut/tag/CAS/

      https://blog.csdn.net/anumbrella/category_7765386.html

      JDBC

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:拉閘限電不用怕,階梯方式算電價
      下一篇:怎樣使用Word來制作課程表(用word怎么做課程表)
      相關文章
      亚洲真人无码永久在线观看| 亚洲成aⅴ人片在线观| 色在线亚洲视频www| 亚洲av之男人的天堂网站| 亚洲另类少妇17p| 亚洲精品国自产拍在线观看 | 精品久久亚洲一级α| 亚洲日韩国产二区无码| 亚洲中文字幕AV每天更新| 97久久国产亚洲精品超碰热| 亚洲制服丝袜第一页| 亚洲夂夂婷婷色拍WW47| 亚洲乱码av中文一区二区| 亚洲另类自拍丝袜第五页| 亚洲精品无码久久久久久| 国产精品亚洲专区一区| mm1313亚洲精品无码又大又粗| 婷婷亚洲综合一区二区| 亚洲精品视频久久久| 国产亚洲精品国看不卡| 亚洲人成图片小说网站| 久久国产精品亚洲综合| 亚洲午夜精品久久久久久人妖| 久久精品国产亚洲AV无码偷窥| 亚洲精品美女在线观看播放| 亚洲人成人77777在线播放| 亚洲AV无码一区二区三区在线| 国产成人精品日本亚洲专| 亚洲欧美日韩中文二区| 香蕉视频亚洲一级| 亚洲日本一区二区一本一道 | 亚洲欧洲中文日韩久久AV乱码 | 狠狠亚洲狠狠欧洲2019| 亚洲国产一二三精品无码| 久久夜色精品国产嚕嚕亚洲av| 亚洲精品韩国美女在线| 精品久久久久久亚洲精品| 亚洲午夜理论片在线观看| 亚洲av无码专区在线电影| 国产av无码专区亚洲国产精品| 日本亚洲视频在线|