网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

使用SpringSecurity怎么自定义一个表单登录功能-创新互联

这期内容当中小编将会给大家带来有关使用SpringSecurity怎么自定义一个表单登录功能,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

10年积累的网站建设、成都做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有独山子免费网站建设让你可以放心的选择与我们合作。

1.创建SpringSecurity项目

1.1 使用IDEA

先通过IDEA 创建一个SpringBoot项目 并且依赖SpringSecurity,Web依赖

使用SpringSecurity怎么自定义一个表单登录功能

此时pom.xml会自动添加


    org.springframework.boot
    spring-boot-starter-security

2.扩展 WebSecurityConfigurerAdapter

WebSecurityConfigurerAdapter 是SpringSecurity 提供的用于我们扩展自己的配置

 实现WebSecurityConfigurerAdapter经常需要重写的:

1、configure(AuthenticationManagerBuilder auth);

2、configure(WebSecurity web);

3、configure(HttpSecurity http);

2.1 默认 WebSecurityConfigurerAdapter 为我们提供了一些基础配置如下

protected void configure(HttpSecurity http) throws Exception {
  logger.debug("Using default configure(HttpSecurity). If subclassed this will potentially override subclass configure(HttpSecurity).");

  http
    .authorizeRequests()
      .anyRequest().authenticated()
      .and()
    .formLogin().and()
    .httpBasic();
}

2.2 创建自定义的 WebSecurityConfigurer

1.formLogin() 开启表单登录,该方法会应用 FormLoginConfigurer 到HttpSecurity上,后续会被转换为对应的Filter
2.loginPage() 配置自定义的表单页面
3.authorizeRequests().anyRequest().authenticated(); 表示任何请求接口都要认证**

@Configuration
@Slf4j
public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {

 

@Override
protected void configure(HttpSecurity http) throws Exception {

    http.csrf().disable()
      .formLogin()
      .loginPage("/mylogin.html")
      .and()
      .authorizeRequests().anyRequest().authenticated();
      
  }
}

2.3 mylogin.html



 
   
   Title
 
 
  

标准登录页面

  

表单登录

                                                   登录           
用户名:
密码:
 

3.访问自定义登录页面(注意有重定向过多问题)

启动项目 并且直接访问

http://localhost:8080

会发现浏览器报 重定向次数过多,这是什么原因呢?

这是因为 我们上面配置了 loginPage("/mylogin.html") ,但是这个路径它没有被允许访问,也就是当重定向到/mylogin.html路径后,还是会因为需要认证 被重定向道 /mylogin.html 导致该错误

使用SpringSecurity怎么自定义一个表单登录功能

4.允许登录页面路径访问 antMatchers("/mylogin.html").permitAll()

只需要在配置的地方 添加 .antMatchers("/mylogin.html").permitAll() 允许这个路径

  http.csrf().disable()
      .formLogin()
      .loginPage("/mylogin.html")
      .and()
      .authorizeRequests()
      .antMatchers("/mylogin.html").permitAll()
      .anyRequest().authenticated();

再次访问,我们自定义的表单就显示出来了(忽略样式。。。)

使用SpringSecurity怎么自定义一个表单登录功能

此时我们输入用户名 user 密码 : 控制台打印

Using generated security password: 6bf253eb-c785-42b6-b147-b0fe2971586e

发现又跳转到 /mylogin.html页面,这是因为 当我们配置了 loginPage("/mylogin.html")之后 处理表单登录的过滤器它所拦截的请求就不再是 /login (默认是 /login) ,拦截的登录请求地址变成了 和 loginPage一样的 mylogin.html

此时如果将 action地址改成 /mylogin.html ,那么再登录 就能成功

5.配置自定义登录接口路径 loginProcessingUrl

由于我们上面配置了 loginPage ,则对应登录接口路径也变成了 loginPage所配置的 mylogin.html,但是当我们不想使用这个作为接口路径的时候,可以通过以下配置来修改

通过 loginProcessingUrl 类配置处理登录请求的路径

 http.csrf().disable()
      .formLogin()
      .loginPage("/mylogin.html")
      .loginProcessingUrl("/auth/login")
      .and()
      .authorizeRequests()
      .antMatchers("/mylogin.html").permitAll()
      .anyRequest().authenticated();

记得和action 对应

上述就是小编为大家分享的使用SpringSecurity怎么自定义一个表单登录功能了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联网站建设公司行业资讯频道。

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页题目:使用SpringSecurity怎么自定义一个表单登录功能-创新互联
URL标题:http://bjjierui.cn/article/eopis.html

其他资讯