• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

SpringBoot Security安装配置及Thymeleaf整合

springboot 搞代码 4年前 (2022-01-05) 32次浏览 已收录 0个评论

这篇文章主要介绍了SpringBoot Security安装配置及Thymeleaf整合,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

功能:解决web站点的登录,权限验证,授权等功能

优点:在不影响站点业务代码,可以权限的授权与验证横切到业务中

1、要添加的依赖

 <!--thymeleaf--> org.springframework.bootspring-boot-starter-thymeleaf<!--security 和 thymeleaf 整合包--> org.thymeleaf.extrasthymeleaf-extras-springsecurity5<!--web--> org.springframework.bootspring-boot-starter-web<!--security--> org.springframework.bootspring-boot-starter-security

2、Security 下授权与验证的简单配置(Security下有登录,注销,记住我等功能,可以快速集成到自己的login页上)
Tis:如果template页中使用了 Frame页,默认是不能访问的,需要添加 http.headers().frameOptions().sameOrigin();

 @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { //授权 @Override protected void configure(HttpSecurity http) throws Exception { //请求授权的规则 http.authorizeRequests() //.antMatchers("/tologin").permitAll() //登录页所有人都可以访问 //.antMatchers("/admin/**").hasRole("admin1") .antMatchers("/admin/list").hasRole("admin1") .antMatchers("/admin/role").hasRole("admin1") .antMatchers("/admin/cate").hasRole("admin2") .antMatchers("/admin/rule").hasRole("admin2"); // 项目里面使用了springSecurity spring Security下,X-Frame-Options默认为DENY,非spring Security环境下,X-Frame-Options的默认大多也是DENY,这种情况下,浏览器拒绝当前页面加载任何Frame页面 http.headers().frameOptions().sameOrigin(); //登录页(Security默认有一个登录页) http.formLogin().permitAll() .loginPage("/tologin") //指定自定义的登录页地址 .successForwardUrl("/admin/index") //登录成功跳转地址 .usernameParameter("username").password<span style="color:transparent">来源gaodai#ma#com搞*代#码网</span>Parameter("password");//匹配自定义登录页的name元素名称 // 开启注销功能,跳转到登录页 http.csrf().disable(); //退出失败可能能的原因 http.logout().logoutSuccessUrl("/tologin"); //开启记住我功能,cookie 默认保存14天 http.rememberMe() .rememberMeParameter("remember");//匹配自定义登录页的name元素名称 } //认证 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .passwordEncoder(new BCryptPasswordEncoder())//密码加密方式(有些版本的Security必须要指定) .withUser("root").password(new BCryptPasswordEncoder().encode("123")).roles("admin1","admin2","admin3") .and() .withUser("yeqiu").password(new BCryptPasswordEncoder().encode("123")).roles("admin1") .and() .withUser("admin").password(new BCryptPasswordEncoder().encode("123")).roles("admin2"); } }

3、Security 和 Thymeleaf 页面整合(添加依赖:thymeleaf-extras-springsecurity)

 <!--加入约束--> <!-- sec:authorize="isAuthenticated()" 用户是否登录 sec:authorize="hasAnyRole('admin1')" 是否具有某个角色 sec:authentication="name" 当前登录用户 sec:authentication="principal.authorities" 当前用户全部角色 --><div> <h2><span></span>,您好 您的身份是 <span></span></h2></div><li>  <cite>权限管理</cite></li>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是SpringBoot Security安装配置及Thymeleaf整合的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringBoot Security安装配置及Thymeleaf整合

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址