chapter09_保护Web应用_4_认证用户

本文详细介绍了如何在Spring Security中配置自定义登录页、启用HttpBasic认证、Remember-me功能及退出登录设置,包括配置代码示例和HTML模板。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 添加自定义的登录页

    (1) 示例

    login.html

      <html xmlns="http://www.w3.org/1999/xhtml"
            xmlns:th="http://www.thymeleaf.org">
      
      ...
    
      <div id="content">
    
          <a th:href="@{/spitter/register}">
              Register
          </a>
          <br/>
          <br/>
    
          <form name='f' th:action='@{/login}' method='POST'>
              <table align="center">
                  <tr>
                      <td>User:</td>
                      <td>
                          <input type='text' name='username' value=''/>
                      </td>
                  </tr>
                  <tr>
                      <td>Password:</td>
                      <td>
                          <input type='password' name='password'/>
                      </td>
                  </tr>
                  <tr>
                      <td colspan='2'>
                          <input id="remember_me" name="remember-me" type="checkbox"/>
                          <label for="remember_me" class="inline">Remember me</label>
                      </td>
                  </tr>
                  <tr>
                      <td colspan='2'>
                          <input name="submit" type="submit" value="Login"/>
                      </td>
                  </tr>
              </table>
          </form>
      </div>
    
      ...
      </html>
    

    SecurityConfig.java

      @Configuration
      @EnableWebMvcSecurity
      public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
          @Override
          protected void configure(HttpSecurity http) throws Exception {
      
              http
                  .formLogin()
                  .loginPage("/login")
                  .and()
                  .authorizeRequests()
                  .antMatchers("/").authenticated()
                  .antMatchers("/spitter/me").authenticated()
                  .antMatchers(HttpMethod.POST, "/spittles").authenticated()
                  .anyRequest().permitAll()
                  .and()
                  .requiresChannel()
                  .anyRequest().requiresInsecure();
          }
    
          ...
      }
    

    (2) 在configure(HttpSecurity)中调用formLogin()即可对登录页面进行各种配置;

    loginPage()指定了登录页的视图名称,再由视图解析器解析到对应的视图中

    (3) login.html中,表单form要提交到相对于上下文的 /login 页面上

  • 启用Http Basic认证

    示例

    SecurityConfig.java

      @Configuration
      @EnableWebMvcSecurity
      public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
          @Override
          protected void configure(HttpSecurity http) throws Exception {
      
              http
                  .formLogin()
                  .loginPage("/login")
                  .and()
                  .httpBasic()
                  .realmName("Spittr")
                  .and()
                  .authorizeRequests()
                  .antMatchers("/").authenticated()
                  .antMatchers("/spitter/me").authenticated()
                  .antMatchers(HttpMethod.POST, "/spittles").authenticated()
                  .anyRequest().permitAll()
                  .and()
                  .requiresChannel()
                  .anyRequest().requiresInsecure();
          }
    
          ...
      }
    

    在configure(HttpSecurity)中添加 .httpBasic().realmName(“Spittr”) 即可开启Http Basic认证,同样使用and进行连接

  • 启用Remember-me功能

    (1) SpringSecurity使得添加Remember-me功能非常简单,直接在configure(HttpSecurity)中添加.rememberMe()即可

    (2) 默认情况下,这个功能是通过在cookie中存储一个token完成的。这个token包含用户名、密码、过期时间和一个私钥,过期时间和私钥可以进行设置。写入cookie之前,这四个属性会经过MD5哈希

    SecurityConfig.java

      @Configuration
      @EnableWebMvcSecurity
      public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
          @Override
          protected void configure(HttpSecurity http) throws Exception {
      
              http
                  .formLogin()
                  .loginPage("/login")
                  .and()
                  .httpBasic()
                  .realmName("Spittr")
                  .and()
                  .rememberMe()
                  .tokenRepository(new InMemoryTokenRepositoryImpl())
                  .tokenValiditySeconds(2419200)  //设置过期时间
                  .key("spittrKey")               //设置私钥名称
                  .and()
                  .authorizeRequests()
                  .antMatchers("/").authenticated()
                  .antMatchers("/spitter/me").authenticated()
                  .antMatchers(HttpMethod.POST, "/spittles").authenticated()
                  .anyRequest().permitAll()
                  .and()
                  .requiresChannel()
                  .anyRequest().requiresInsecure();
          }
    
          ...
      }
    

    (4) 对应的,html中__必须包括一个名为remember-me的参数__与之对应

    login.html

      <html xmlns="http://www.w3.org/1999/xhtml"
            xmlns:th="http://www.thymeleaf.org">
      
      ...
    
      <div id="content">
    
          <a th:href="@{/spitter/register}">
              Register
          </a>
          <br/>
          <br/>
    
          <form name='f' th:action='@{/login}' method='POST'>
              <table align="center">
                  <tr>
                      <td>User:</td>
                      <td>
                          <input type='text' name='username' value=''/>
                      </td>
                  </tr>
                  <tr>
                      <td>Password:</td>
                      <td>
                          <input type='password' name='password'/>
                      </td>
                  </tr>
                  <tr>
                      <td colspan='2'>
                          <input id="remember_me" name="remember-me" type="checkbox"/>
                          <label for="remember_me" class="inline">Remember me</label>
                      </td>
                  </tr>
                  <tr>
                      <td colspan='2'>
                          <input name="submit" type="submit" value="Login"/>
                      </td>
                  </tr>
              </table>
          </form>
      </div>
    
      ...
      </html>
    
  • 退出

    (1) 默认情况下,退出功能是SpringSecurity Filter实现的,这个Filter会拦截/logout的请求

    (2) 退出的设置也是在 configure(HttpSecurity)方法中,调用logout()方法,同样使用and()连接不同的配置

    SecurityConfig.java

      @Configuration
      @EnableWebMvcSecurity
      public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
          @Override
          protected void configure(HttpSecurity http) throws Exception {
      
              http
                  .formLogin()
                  .loginPage("/login")
                  .and()
                  .httpBasic()
                  .realmName("Spittr")
                  .and()
                  .rememberMe()
                  .tokenRepository(new InMemoryTokenRepositoryImpl())
                  .tokenValiditySeconds(2419200)  //设置过期时间
                  .key("spittrKey")               //设置私钥名称
                  .and()
                  .logout()
                  .logoutSuccessUrl("/")
                  .logoutUrl("/logout")
                  .and()
                  .authorizeRequests()
                  .antMatchers("/").authenticated()
                  .antMatchers("/spitter/me").authenticated()
                  .antMatchers(HttpMethod.POST, "/spittles").authenticated()
                  .anyRequest().permitAll()
                  .and()
                  .requiresChannel()
                  .anyRequest().requiresInsecure();
          }
    
          ...
      }
    

    (3) logoutSuccessUrl用于设置退出登录后重定向的url;

    logoutUrl用于设置Filter的拦截路径(默认拦截"/logout"的请求)

内容概要:该论文研究增程式电动汽车(REEV)的能量管理策略,针对现有优化策略实时性差的问题,提出基于工况识别的自适应等效燃油消耗最小策略(A-ECMS)。首先建立整车Simulink模型和基于规则的策略;然后研究动态规划(DP)算法和等效燃油最小策略;接着通过聚类分析将道路工况分为四类,并设计工况识别算法;最后开发基于工况识别的A-ECMS,通过高德地图预判工况类型并自适应调整SOC分配。仿真显示该策略比规则策略节油8%,比简单SOC规划策略节油2%,并通过硬件在环实验验证了实时可行性。 适合人群:具备一定编程基础,特别是对电动汽车能量管理策略有兴趣的研发人员和技术爱好者。 使用场景及目标:①理解增程式电动汽车能量管理策略的基本原理;②掌握动态规划算法和等效燃油消耗最小策略的应用;③学习工况识别算法的设计和实现;④了解基于工况识别的A-ECMS策略的具体实现及其优化效果。 其他说明:此资源不仅提供了详细的MATLAB/Simulink代码实现,还深入分析了各算法的原理和应用场景,适合用于学术研究和工业实践。在学习过程中,建议结合代码调试和实际数据进行实践,以便更好地理解策略的优化效果。此外,论文还探讨了未来的研究方向,如深度学习替代聚类、多目标优化以及V2X集成等,为后续研究提供了思路。
内容概要:论文《基于KANN-DBSCAN带宽优化的核密度估计载荷谱外推》针对传统核密度估计(KDE)载荷外推中使用全局固定带宽的局限性,提出了一种基于改进的K平均最近邻DBSCAN(KANN-DBSCAN)聚类算法优化带宽选择的核密度估计方法。该方法通过对载荷数据进行KANN-DBSCAN聚类分组,采用拇指法(ROT)计算各簇最优带宽,再进行核密度估计和蒙特卡洛模拟外推。实验以电动汽车实测载荷数据为对象,通过统计参数、拟合度和伪损伤三个指标验证了该方法的有效性,误差显著降低,拟合度R²>0.99,伪损伤接近1。 适合人群:具备一定编程基础和载荷数据分析经验的研究人员、工程师,尤其是从事汽车工程、机械工程等领域的工作1-5年研发人员。 使用场景及目标:①用于电动汽车载荷谱编制,提高载荷预测的准确性;②应用于机械零部件的载荷外推,特别是非对称载荷分布和多峰扭矩载荷;③实现智能网联汽车载荷预测与数字孪生集成,提供动态更新的载荷预测系统。 其他说明:该方法不仅解决了传统KDE方法在复杂工况下的“过平滑”与“欠拟合”问题,还通过自适应参数机制提高了方法的普适性和计算效率。实际应用中,建议结合MATLAB代码实现,确保数据质量,优化参数并通过伪损伤误差等指标进行验证。此外,该方法可扩展至风电装备、航空结构健康监测等多个领域,未来研究方向包括高维载荷扩展、实时外推和多物理场耦合等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值