当前位置: 首页 > news >正文

烟台网站建设托管网站分析实例

烟台网站建设托管,网站分析实例,深圳电力建设公司,杭州工作招聘网文章目录 1、thymeleaf2、依赖部分3、定义Controller4、创建静态页面5、WebSecurityConfigurerAdapter6、权限相关7、当用户没有某权限时#xff0c;页面不展示该按钮 1、thymeleaf 查了下读音#xff0c;leaf/li:f/#xff0c;叶子#xff0c;前面的单词发音和时间time一… 文章目录 1、thymeleaf2、依赖部分3、定义Controller4、创建静态页面5、WebSecurityConfigurerAdapter6、权限相关7、当用户没有某权限时页面不展示该按钮 1、thymeleaf 查了下读音leaf/li:f/叶子前面的单词发音和时间time一样。 官网https://www.thymeleaf.org/参考中文文档https://fanlychie.github.io/post/thymeleaf.html Thymeleaf is a modern server-side Java template engine for both web and standalone environments. 即Thymeleaf是适用于Web和独立环境的现代服务器端Java 模板引擎 。模板引擎的作用就是使用户界面与业务数据内容分离就是做好一个模板后套入对应位置的数据最终以html的格式展示出来。知乎上有个很形象的例子 简单说就是没模板引擎就像高中操场开会桌子、板凳、场地都要现搬现搭。而模板引擎的作用就像大学开会有专门会议室板凳桌子设备都准备好了今天学院A进来用了明天学院B进来用了学院A、B就像数据。 将模板设计好之后直接填充数据即可而不需要重新设计整个页面开箱即用提高页面、代码的复用性。 市面上开源的第三方的模板引擎也比较多有Thymeleaf、FreeMaker、Velocity等 2、依赖部分 引入thymeleaf依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-thymeleaf/artifactId /dependency 当然如果是新创建项目直接勾选热门依赖就行 修改配置文件 spring:thymeleaf:cache: false # 开发阶段可以先不使用缓存check-template: true # 检查thymeleaf模板是否存在 之所以不使用缓存是为了临时有改动时点一下小锤子就能看效果 在IDEA添加thymeleaf文件模板方便以后使用File-Setting 模板名称thymeleaf 扩展名html内容如下 !DOCTYPE html html xmlns:thhttp://www.thymeleaf.org head meta charsetUTF-8 title#[[$Title$]]#/title /head body #[[$END$]]# /body /html PS #[[$Title$]]# #[[$END$]]# 这两处的作用是 当你新建一个模板页面时在title标签中输入标题内容后只需要点击回车键光标就会直接跳到body内省去了你挪动鼠标或者挪动方向键的步骤也可以给你节省一点点时间。也可在IDEA中安装html转thymeleaf的插件 3、定义Controller //这里别用RestController了不再返回一个json对象或者普通字符串了 Controller RequestMapping(/login) public class LoginController {/*** 跳转到登陆页面*/RequestMapping(/toLogin) //GET、POST都行的意思public String toLogin(){return login;}} 上面的这个return login字符串是返回thymeleaf的逻辑视图名物理视图 前缀 逻辑视图 后缀即/templates/ login .html(点住application.yaml文件中thymeleaf的配置查看源码 再定义登录成功后进入主页的controller返回逻辑视图名main随便起的 Controller RequestMapping(/index) public class IndexController {/*** 登录成功后进入主页*/RequestMapping(/toIndex)public String toIndex(){return main;} } 4、创建静态页面 在templates下面创建login.html和main.html放到类路径中的resource下面 !DOCTYPE html html xmlns:thhttp://www.thymeleaf.org headmeta charsetUTF-8title用户登陆/title /head body h2登录页面/h2 form action/login/doLogin methodposttabletrtd用户名:/tdtdinput typetext nameuname valuethomas/td/trtrtd密码:/tdtdinput typepassword namepwd/tdspan th:if${param.error}用户名或者密码错误/span/trtrtd colspan2button typesubmit登录/button/td/tr/table /form /body mian.html内容 !DOCTYPE html html langen headmeta charsetUTF-8title系统首页/title /head body h1 aligncenter系统首页/h1 a href/student/query查询学生/a br a href/student/add添加学生/a br a href/student/update更新学生/a br a href/student/delete删除学生/a br a href/student/export导出学生/a br brbrbr h2a href/logout退出/a/h2 br /body /html 5、WebSecurityConfigurerAdapter 修改安全配置类 EnableGlobalMethodSecurity(prePostEnabled true) //Configuration Slf4j public class WebSecurityConfig extends WebSecurityConfigurerAdapter {//编码器Beanpublic PasswordEncoder passwordEncoder(){return new BCryptPasswordEncoder();}Overrideprotected void configure(HttpSecurity http) throws Exception {//设置登陆方式http.formLogin()//使用用户名和密码的登陆方式.usernameParameter(uname) //页面表单的用户名的name上面login.html中定义的用户名的参数名.passwordParameter(pwd)//页面表单的密码的password上面login.html中定义的密码的参数名.loginPage(/login/toLogin) //自己定义登陆页面的地址.loginProcessingUrl(/login/doLogin)//配置登陆的url.successForwardUrl(/index/toIndex) //登陆成功跳转的页面成功跳首页.failureForwardUrl(/login/toLogin)//登陆失败跳转的页面失败跳登录页.permitAll();//配置退出方式http.logout().logoutUrl(/logout).logoutSuccessUrl(/login/toLogin).permitAll();//配置路径拦截 的url的匹配规则http.authorizeRequests()//任何路径要求必须认证之后才能访问.anyRequest().authenticated();// 先禁用csrf跨站请求攻击保护 后面可以使用postman工具测试注意要禁用csrfhttp.csrf().disable();} } 此时登录后可以跳转首页了 6、权限相关 修改上一篇中的StudentController写接口返回不同的逻辑视图名称字符串。并给接口加权限校验。 Controller //返回的不是一个字符串是一个视图名 Slf4j RequestMapping(/student) public class StudentController {GetMapping(/query)PreAuthorize(hasAuthority(student:query))public String queryInfo(){return user/query; //:templates/ user/query .html }GetMapping(/add)PreAuthorize(hasAuthority(student:add))public String addInfo(){return user/add;}GetMapping(/update)PreAuthorize(hasAuthority(student:update))public String updateInfo(){return user/update;}GetMapping(/delete)PreAuthorize(hasAuthority(student:delete))public String deleteInfo(){return user/delete;}GetMapping(/export)PreAuthorize(hasAuthority(student:export))public String exportInfo(){return /user/export;} } 在templates/user下面创建学生管理的各个页面export.html !DOCTYPE html html langen xmlns:thhttp://www.thymeleaf.orgheadmeta charsetUTF-8title系统首页-学生管理/title /head body h1 aligncenter系统首页-学生管理-导出/h1 a href/index/toIndex返回/a br /body /html add.html !DOCTYPE html html langen headmeta charsetUTF-8title系统首页-学生管理/title /head body h1 aligncenter系统首页-学生管理-新增/h1 a href/index/toIndex返回/a br /body /html update.html !DOCTYPE html html langen headmeta charsetUTF-8title系统首页-学生管理/title /head body h1 aligncenter系统首页-学生管理-更新/h1 a href/index/toIndex返回/a br /body /html delete.html !DOCTYPE html html langen headmeta charsetUTF-8title系统首页-学生管理/title /head body h1 aligncenter系统首页-学生管理-删除/h1 a href/index/toIndex返回/a br /body /html query.html !DOCTYPE html html langen headmeta charsetUTF-8title系统首页-学生管理/title /head body h1 aligncenter系统首页-学生管理-查询/h1 a href/index/toIndex返回/a br /body /html 在static/error下面创建403.html当没有权限的时候就会使用这里的403页面代替框架自带的403页面 !DOCTYPE html html langen headmeta charsetUTF-8title403/title /head body h2403:你没有权限访问此页面/h2 a href/index/toIndex去首页/a /body /html 查看效果 7、当用户没有某权限时页面不展示该按钮 当用户点击页面上的链接请求到后台之后没有权限会跳转到403那么如果用户没有权限对应的按钮就不显示出来这样岂不是更好吗。下面开始实现 引入依赖 dependencygroupIdorg.thymeleaf.extras/groupIdartifactIdthymeleaf-extras-springsecurity5/artifactId /dependency 修改首页代码在标签的sec属性中加入对应的所需权限 !DOCTYPE html html langen xmlns:thhttp://www.thymeleaf.orgxmlns:sechttp://www.thymeleaf.org/extras/spring-security headmeta charsetUTF-8title系统首页/title /head body h1 aligncenter系统首页/h1 a href/student/query sec:authorizehasAuthority(student:query) 查询用户/a br a href/student/add sec:authorizehasAuthority(student:save) 添加用户/a br a href/student/update sec:authorizehasAuthority(student:update) 更新用户/a br a href/student/delete sec:authorizehasAuthority(student:delete) 删除用户/a br a href/student/export sec:authorizehasAuthority(student:export) 导出用户/a br brbrbr h2a href/logout退出/a/h2 br /body /html 重启此时的效果
http://www.dnsts.com.cn/news/79311.html

相关文章:

  • 厦门网站建设培训班义乌网站建站
  • 企业所得税计算方式seo网站推广招聘
  • 建设网站项目的目的是什么中国机械加工最多的地方
  • 网站的营销吉林网站建设找哪家
  • 迪庆企业网站建设公司展览网站模板大全
  • 自助式建网站网络优化工程师主要负责什么工作
  • 商务网站的主要内容重庆城乡和住房建设信息网
  • 网页设计与网站建设 郑州大学无锡市网站
  • 怎么给喜欢的人做网站网站建设认准猪八戒网
  • 快速做网站公司报价台州网站制作计划
  • 网站必须做诚信认证吗同一ip 网站 权重
  • 广东网站建设方案报价绍兴劳务公司
  • 惠通网站建设wordpress 自定义字段 调用
  • 计算机专业的会学怎么做网站吗网页设计的ppt展示
  • 外贸云网站建设域名提交收录
  • 网站信息发布系统房地产开发公司职位
  • 风铃网站具体是做那方面的现在lol谁做教学视频网站
  • 0731网站沈阳seo全网营销
  • 线在科技成都网站推广公司网站地图模板下载
  • ftp可以发布网站吗北京公司摇号中签率
  • 三合一网站开发文山知名网站建设报价
  • 51的网站是啥线上编程哪家比较好
  • 网站关键词多少个好重庆网站建设
  • nginx网站开发网站设计制作价格怎么算
  • 招投标网站韩国优秀网站欣赏
  • 网站模版防被偷aiyuan wordpress
  • 自建网站营销建立中国近代美育体系的人是谁
  • 河南做网站哪家好网站开发工程师月薪
  • ftp链接网站空间创业项目大全
  • dede一键更新网站建筑业招聘信息平台