栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

使用SpringSecurity

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用SpringSecurity

前几天写了一个SpringBoot对拦截器的使用,在实际项目中,对一些情况需要做一些安全验证,比如在没有登录的情况下访问特定的页面应该解释的拦截处理。这一篇介绍使用SpringSecurity来做简单的安全控制,由于SpringSecurity比较复杂,如果有不对的地方可以大家一起学习。

新建项目,前端页面使用thymeleaf,加入security依赖,pom文件如下:



    4.0.0

    com.dalaoyang
    springboot_security
    0.0.1-SNAPSHOT
    jar

    springboot_security
    springboot_security

    
 org.springframework.boot
 spring-boot-starter-parent
 1.5.9.RELEASE
  
    

    
 UTF-8
 UTF-8
 1.8
    

    
 
     org.springframework.boot
     spring-boot-starter-web
 

 
     org.springframework.boot
     spring-boot-devtools
     runtime
 
 
     org.springframework.boot
     spring-boot-starter-test
     test
 
 
     org.springframework.boot
     spring-boot-starter-security
 
 
     net.sourceforge.nekohtml
     nekohtml
     1.9.15
 
 
     org.springframework.boot
     spring-boot-starter-thymeleaf
 
    

    
 
     
  org.springframework.boot
  spring-boot-maven-plugin
     
 
    

配置文件本文就是将之前整合thymeleaf的配置拿了过来,代码如下:

##端口号
server.port=8888

##去除thymeleaf的html严格校验
spring.thymeleaf.mode=LEGACYHTML5

#设定thymeleaf文件路径 默认为src/main/resources/templates
spring.freemarker.template-loader-path=classpath:/templates
#设定静态文件路径,js,css等
spring.mvc.static-path-pattern=/static
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    // /css
@Controller
public class TestController {

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

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

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

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

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

    @RequestMapping("/login_error")
    public String login_error(Model model){
 model.addAttribute("login_error", "用户名或密码错误");
 return "login";
    }

    @RequestMapping("/logout")
    public String logout(Model model){
 model.addAttribute("login_error", "注销成功");
 return "login";
    }

    @RequestMapping("/401")
    public String error(){
 return "401";
    }
}

创建一个user/index.html,用于校验USER权限,没有登录的话不能直接访问,代码如下:




    
    user/index


user/index



创建一个admin/index.html,只允许ADMIN权限访问,代码如下:




    
    admin


admin/index

401页面,用于没有权限跳转:




    
    401


401

index页面,任何权限都能访问




    
    index


index

login页面,用于登录




    
    login


login


到这里就全部创建完成了,启动项目,访问http://localhost:8888/,如图,可以直接访问。

访问http://localhost:8888/user被拦截到http://localhost:8888/login,如图

先输入错误的密码,如图

然后输入用户名dalaoyang密码123,点击登录结果如图

访问http://localhost:8888/admin,如图,没有权限

我们在回到http://localhost:8888/user点击注销,如图

源码下载 :大老杨码云

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235396.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号