SpringBoot:集成SpringSecurity

1.新建一个springboot项目

 

 2.编写一个controller和引入相关静态资源

 

 controller如下:

package com.yao.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class RouterController {

    @RequestMapping({"/","/index"})
    public String index(){
        return "index";
    }

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "views/login";
    }

    @RequestMapping("/level1/{id}")
    public String level1(@PathVariable("id") int id){
        return "views/level1/"+id;
    }

    @RequestMapping("/level2/{id}")
    public String level2(@PathVariable("id") int id){
        return "views/level2/"+id;
    }

    @RequestMapping("/level3/{id}")
    public String level3(@PathVariable("id") int id){
        return "views/level3/"+id;
    }
}

3.导入security依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

 

 

4.创建一个config,并编写

 

 config如下:

package com.yao.config;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity//已经被springboot托管了
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    //链式编程
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //首页所有人可以访问,但是里面的功能页只有对应有权限的人才可以访问

        //1.认证请求
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");

    }
}

在configure()方法中加入以下配置,开启自动配置的登录功能

package com.yao.config;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


//AOP:拦截器
@EnableWebSecurity//已经被springboot托管了
public class SecurityConfig extends WebSecurityConfigurerAdapter {

//链式编程
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人可以访问,但是里面的功能页只有对应有权限的人才可以访问

//1.认证请求
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");


//没有权限默认到登录页
http.formLogin();
}
这里会跳到:

 

 继续完善config的认证功能

 

 最后完整代码:

package com.yao.config;

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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;


//AOP:拦截器
@EnableWebSecurity//已经被springboot托管了
public class SecurityConfig extends WebSecurityConfigurerAdapter {

//链式编程


//授权
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人可以访问,但是里面的功能页只有对应有权限的人才可以访问

//1.请求权限规则
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");


//没有权限默认到登录页
http.formLogin();
}


//认证,spring 2.1.x 可以直接使用
//密码编码:PassWordEncoder
//在Spring Secutiry 新增了很多加密方式
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//这里因为没有连接数据库所以在内存中去认证,下面的数据应该从数据库里读
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("yaoyao").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
.and()
.withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
.and()
.withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
}
}

  

5.增加注销功能

5.1在config中添加:

 

 5.2在前端页面中添加注销功能按钮

 

 

 

 基本功能完成了,我们再去完善一下注销功能,改动一下

 

 6.实现不同权限的用户显示不同的模块,thymeleaf和SpringSecurity整合

6.1导入对应的依赖

Maven依赖:

<!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 -->
<dependency>
  <groupId>org.thymeleaf.extras</groupId>
  <artifactId>thymeleaf-extras-springsecurity5</artifactId>
  <version>3.0.4.RELEASE</version>
</dependency>

7、修改我们的 前端页面

  1. 导入命名空间

  2. xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"
  3. 修改导航栏,增加认证判断

  4. <!--登录注销-->
    <div class="right menu">

      <!--如果未登录-->
      <div sec:authorize="!isAuthenticated()">
          <a class="item" th:href="@{/login}">
              <i class="address card icon"></i> 登录
          </a>
      </div>

      <!--如果已登录-->
      <div sec:authorize="isAuthenticated()">
          <a class="item">
              <i class="address card icon"></i>
            用户名:<span sec:authentication="principal.username"></span>
            角色:<span sec:authentication="principal.authorities"></span>
          </a>
      </div>

      <div sec:authorize="isAuthenticated()">
          <a class="item" th:href="@{/logout}">
              <i class="address card icon"></i> 注销
          </a>
      </div>
    </div>

8.如果注销404了,就是因为它默认防止csrf跨站请求伪造,因为会产生安全问题,我们可以将请求改为post表单提交,或者在spring security中关闭csrf功能;我们试试:在 配置中增加 http.csrf().disable();

http.csrf().disable();//关闭csrf功能:跨站请求伪造,默认只能通过post方式提交logout请求
http.logout().logoutSuccessUrl("/");

 9.继续完成index。html页面显示

 

 

 

 10.记住我功能的实现

在config类中继续编写:

 

 

 

 

 11.定制登录页

11.1、在刚才的登录页配置后面指定 loginpage

http.formLogin().loginPage("/toLogin");

11.2、然后前端也需要指向我们自己定义的 login请求

<a class="item" th:href="@{/toLogin}">
  <i class="address card icon"></i> 登录
</a>

11.3、我们登录,需要将这些信息发送到哪里,我们也需要配置,login.html 配置提交请求及方式,方式必须为post:

在 loginPage()源码中的注释上有写明:

img

<form th:action="@{/login}" method="post">
  <div class="field">
      <label>Username</label>
      <div class="ui left icon input">
          <input type="text" placeholder="Username" name="username">
          <i class="user icon"></i>
      </div>
  </div>
  <div class="field">
      <label>Password</label>
      <div class="ui left icon input">
          <input type="password" name="password">
          <i class="lock icon"></i>
      </div>
  </div>
  <input type="submit" class="ui blue submit button"/>
</form>

4、这个请求提交上来,我们还需要验证处理,怎么做呢?我们可以查看formLogin()方法的源码!我们配置接收登录的用户名和密码的参数!

http.formLogin()
.usernameParameter("username")
.passwordParameter("password")
.loginPage("/toLogin")
.loginProcessingUrl("/login"); // 登陆表单提交请求

5、在登录页增加记住我的多选框

<input type="checkbox" name="remember"> 记住我

6、后端验证处理!

//定制记住我的参数!
http.rememberMe().rememberMeParameter("remember");

详情见spring-07练习

 

posted @ 2021-01-06 15:03  Yaoyaoo  阅读(250)  评论(0)    收藏  举报