SpringBoot整合SpringSecurity


SpringSecurity整合到SpringBoot项目中

  • 首先创建的项目如图所示

image.png

1.在pom.xml中导入SpringSecurity的依赖

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

2.导入thymeleaf模板引擎和静态文件

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

3.编写后端controller

package com.cxy.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(value = {"/index","/"})
  public String index(){
    return "index";
  }
  @RequestMapping(value = {"/toLogin"})
  public String toLogin(){
    return "/views/login";
  }

  // 根据id和level获取不同的控制访问路径 
  @RequestMapping("/level1/{id}")
  public String toLevel1(@PathVariable("id") int id){
    return "/views/level1/"+id;
  }
  @RequestMapping("/level2/{id}")
  public String toLevel2(@PathVariable("id") int id){
    return "/views/level2/"+id;
  }
  @RequestMapping("/level3/{id}")
  public String toLevel3(@PathVariable("id") int id){
    return "/views/level3/"+id;
  }
}

4.编写配置文件用于映射请求以及用户登录状态,查看不同的权限

  • 这个文件夹在config下面
package com.cxy.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;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//  授权 这里是链式编程,跟前端中的promise函数有点相像
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    //首页所有人可以访问,功能页具有权限的才能进行访问
    http.authorizeRequests().antMatchers("/")
            .permitAll()
            .antMatchers("/level1/**").hasRole("vip1")
            .antMatchers("/level2/**").hasRole("vip2")
            .antMatchers("/level3/**").hasRole("vip3");
    //关闭默认的cfrs防止网站攻击  登出失败的这个
    http.csrf().disable();
    //跳转到内置写好的页面 添加前端表单上的username和password属性的别名
    http.formLogin().loginPage("/toLogin").loginProcessingUrl("/login")
            .usernameParameter("user")
            .passwordParameter("pwd");
    //开启注销功能
    http.logout().logoutSuccessUrl("/");
    //开启记住我功能
    http.rememberMe().rememberMeParameter("remember");
  }

//  认证
//  要进行密码进行加密
//  使用spring security中的password编码格式
  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    //这里应该是从数据库中查找数据
    auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
            .withUser("cxy").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2")
    .and()
    .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
    .and()
    .withUser("chest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
  }
}

5.开启spring boot项目,访问首页,进入登录页

image.png

5.1因为写了chest来宾用户只有VIP1 的一个权限,所以登录之后只能进行操作VIP1 相关的内容

  • level1的操作是正常的

image.png

  • level2的操作就是没有授权的

image.png

这就做到了不同的用户操作不同的内容,而且显示的东西也是不一样的


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 1371769065@qq.com

文章标题:SpringBoot整合SpringSecurity

字数:770

本文作者:一叶知秋

发布时间:2020-07-08, 00:16:14

最后更新:2020-07-08, 22:50:35

原始链接:http://yoursite.com/2020/07/08/springboot/SpringBoot%E6%95%B4%E5%90%88SpringSecurity/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

×

喜欢就点赞,疼爱就打赏

相册 github