CAS实战(cas-server-3.3.4 + cas-client-3.1.3)三

} catch (Exception ignored) {}

        }

    }

 

    /**

     * Calculate digest of given String using given algorithm.

     * Encode digest in MIME-like base64.

     *

     * @param pass the String to be hashed

     * @param algorithm the algorithm to be used

     * @return String Base-64 encoding of digest

     *

     * @throws NoSuchAlgorithmException if the algorithm passed in cannot be found

     */

    public static String digestString(String pass, String algorithm )

            throws NoSuchAlgorithmException  {

 

        MessageDigest md;

        ByteArrayOutputStream bos;

 

        try {

            md = MessageDigest.getInstance(algorithm);

            byte[] digest = md.digest(pass.getBytes("iso-8859-1"));

            bos = new ByteArrayOutputStream();

            OutputStream encodedStream = MimeUtility.encode(bos, "base64");

            encodedStream.write(digest);

            return bos.toString("iso-8859-1");

        } catch (IOException ioe) {

            throw new RuntimeException("Fatal error: " + ioe);

        } catch (MessagingException me) {

            throw new RuntimeException("Fatal error: " + me);

        }

    }

 

    /**

     * Private constructor to prevent instantiation of the class

     */

    private DigestUtil() {}

   

}

Spring Boot Starter Security是一个轻量级的框架,用于快速集成Spring Security到Spring Boot应用程序中。版本3.3.4提供了一系列的安全功能,包括身份验证、授权、会话管理等。以下是使用Spring Boot Starter Security的一个简单教程: 1. **添加依赖**: 在你的`pom.xml`或`build.gradle`文件中添加Spring Security的相关依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 2. **配置应用安全**: 创建一个`SecurityConfig.java`类并实现WebSecurityConfigurerAdapter,设置默认的登录页面和注销路径: ```java 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.WebSecurityConfigurerAdapter; @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/", "/login").permitAll() .anyRequest().authenticated() .and() .formLogin().loginPage("/login") .logout().logoutUrl("/logout"); } // 如果需要,可以在此配置用户认证 @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("user").password("{noop}password").roles("USER"); } } ``` 3. **创建登录界面**: 创建一个简单的HTML登录表单,并将其放在`src/main/resources/static/login.html`文件中。 4. **启动应用**: 使用Spring Boot命令行工具运行应用,访问`http://localhost:8080/login`尝试登录。 5. **自定义授权规则**: 如果你需要更复杂的权限控制,可以在`configure()`方法中使用`accessDecisionManager()`,或者使用基于注解的声明式安全控制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值