Spring switch的使用

首先在html开始标签中引入一个属性

1
xmlns:th= "http://www.thymeleaf.org"

示例代码

   <div th:switch="${users}">
       <h2 th:case="null">没有数据</h2>
	   <div th:case="*">
   </div>

th:switch相当于java的switch语句

th:case相当java的case语句

case的值写在引号里面

转载于:https://www.cnblogs.com/max-hou/p/11098571.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 中的 SwitchUserFilter 是一个安全过滤器,它允许管理员切换到另一个用户的身份进行测试或调试。下面是一个简单的 SwitchUserFilter 配置示例: 1. 在 pom.xml 中添加 spring-security-web 依赖: ```xml <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>5.4.2</version> </dependency> ``` 2. 在 Spring Security 配置类中启用 SwitchUserFilter: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/admin/**").hasRole("ADMIN") .anyRequest().authenticated() .and() .formLogin() .and() .logout() .and() .csrf().disable() .addFilterAfter(switchUserFilter(), FilterSecurityInterceptor.class); } private SwitchUserFilter switchUserFilter() { SwitchUserFilter filter = new SwitchUserFilter(); filter.setUserDetailsService(userDetailsService()); filter.setSuccessHandler(switchUserSuccessHandler()); filter.setFailureHandler(authenticationFailureHandler()); return filter; } private AuthenticationSuccessHandler switchUserSuccessHandler() { return (request, response, authentication) -> { // 切换成功后的逻辑 }; } private AuthenticationFailureHandler authenticationFailureHandler() { return (request, response, exception) -> { // 切换失败后的逻辑 }; } @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") .and() .withUser("admin").password(passwordEncoder().encode("password")).roles("ADMIN"); } } ``` 在这个示例中,我们配置了两个用户,一个是普通用户,一个是管理员。管理员可以通过访问 `/admin/switch` 接口来切换到其他用户的身份。 3. 在控制器中添加切换用户的接口: ```java @RestController @RequestMapping("/admin") public class AdminController { @GetMapping("/switch") public void switchUser(HttpServletRequest request, String username) { try { request.login(username, "password"); } catch (ServletException e) { // 登录失败的逻辑 } } } ``` 在这个示例中,我们通过 `request.login(username, "password")` 方法来切换用户的身份。注意,这个方法只能在控制器中使用,不能在过滤器中使用

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值