spring boot+freemarker+spring security标签权限判断
spring boot+freemarker+spring security标签权限判断
SpringBoot+SpringSecurity+Freemarker项目中在页面上使用security标签控制按钮显示隐藏达到对按钮级权限控制还是比较方便的,如下配置即可。
1、引入依赖
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
</dependency> <dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2.1-b03</version>
</dependency>
2、依赖引入后到spring-security-taglibs包中META-INF下security.tld复制出来,放到/resources/下,最后建一个目录tags,如下:

3、建一个配置类:ClassPathTldsLoader.java
import java.util.Arrays;
import java.util.List; import javax.annotation.PostConstruct; import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; public class ClassPathTldsLoader { /**
* 指定路径
*/
private static final String SECURITY_TLD = "/security.tld"; final private List<String> classPathTlds; public ClassPathTldsLoader(String... classPathTlds) {
super();
if(ArrayUtils.isEmpty(classPathTlds)){
this.classPathTlds = Arrays.asList(SECURITY_TLD);
}else{
this.classPathTlds = Arrays.asList(classPathTlds);
}
}
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer; @PostConstruct
public void loadClassPathTlds() {
freeMarkerConfigurer.getTaglibFactory().setClasspathTlds(classPathTlds);
}
}
4.然后在网站配置文件SecurityConfig.java中加入bean
/**
* 自动加载security-taglibs
* @return
*/
@Bean
@ConditionalOnMissingBean(ClassPathTldsLoader.class)
public ClassPathTldsLoader classPathTldsLoader(){
return new ClassPathTldsLoader();
}
参考:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
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.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder; @EnableWebSecurity
//启用全局post安全方法设置
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter { private static final String key = "muyang.my"; @Autowired
private UserDetailsService userDetailsService; @Bean
public PasswordEncoder passwordEncoder()
{
return new BCryptPasswordEncoder(); } @Bean
public AuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
authenticationProvider.setUserDetailsService(userDetailsService);
//密码加密方式
authenticationProvider.setPasswordEncoder(passwordEncoder());
return authenticationProvider; } @Override
protected void configure(HttpSecurity http) throws Exception {
// TODO Auto-generated method stub
//super.configure(http);
//关闭csrf验证:跨站攻击
//http.csrf().disable();
//权限设置
http.authorizeRequests() //定义那些url需要保护,哪些不需要保护
.antMatchers("/static/**").permitAll() //都可以访问
.antMatchers("/user/**").hasRole("ADMIN") //需要登陆才能访问
.and()
.headers().frameOptions().disable() //解决js跨站把x-frame-options disable即可
.and()
.formLogin() //基于FORM表单登陆验证
.loginPage("/login").failureUrl("/login-error") //自定义登陆界面//自定义登陆错误页面
.and().rememberMe().key(key) //记住我
.and().exceptionHandling().accessDeniedPage("/403"); // 处理异常,拒绝访问就重定向到 403 页面
} /**
* 认证信息管理
* @param auth
* @throws Exception
*/
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
// TODO Auto-generated method stub
//super.configure(auth);
//auth.inMemoryAuthentication().withUser("admin").password("123456").roles("ADMIN");
auth.userDetailsService(userDetailsService);
auth.authenticationProvider(authenticationProvider());
} /**
* 自动加载security-taglibs
* @return
*/
@Bean
@ConditionalOnMissingBean(ClassPathTldsLoader.class)
public ClassPathTldsLoader classPathTldsLoader(){
return new ClassPathTldsLoader();
} }
5、在freemarker页面顶部引入标签
<#assign security=JspTaglibs["http://www.springframework.org/security/tags"] />
使用标签
<@security.authorize access="hasRole('ADMIN')">
222
</@security.authorize>
6.或者
<%@taglib uri="http://www.springframework.org/security/tags" prefix="sec"%>
<sec:authorize access="isAuthenticated()">
<% response.sendRedirect("main"); %>
</sec:authorize>
spring boot+freemarker+spring security标签权限判断的更多相关文章
- Spring boot Freemarker 获取ContextPath的方法
Spring boot Freemarker 获取ContextPath的两种方法: 1.自定义viewResolver,Spring boot中有一个viewResolver,这个和配置文件中的师徒 ...
- Spring Boot Freemarker特别篇之contextPath【从零开始学Spring Boot】(转)
需求缘起:有人在群里@我:请教群主大神一个问题,spring boot + freemarker 怎么获取contextPath 头疼死我了,网上没一个靠谱的 .我就看看之前博客中的 [Spring ...
- Spring Boot Freemarker特别篇之contextPath【从零开始学Spring Boot
需求缘起:有人在群里@我:请教群主大神一个问题,spring boot + freemarker 怎么获取contextPath 头疼死我了,网上没一个靠谱的 .我就看看之前博客中的 [Spri ...
- 基于Spring Boot和Spring Cloud实现微服务架构学习
转载自:http://blog.csdn.net/enweitech/article/details/52582918 看了几周Spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习感 ...
- 基于Spring Boot和Spring Cloud实现微服务架构学习--转
原文地址:http://blog.csdn.net/enweitech/article/details/52582918 看了几周spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习 ...
- 一:Spring Boot、Spring Cloud
上次写了一篇文章叫Spring Cloud在国内中小型公司能用起来吗?介绍了Spring Cloud是否能在中小公司使用起来,这篇文章是它的姊妹篇.其实我们在这条路上已经走了一年多,从16年初到现在. ...
- 基于Spring Boot、Spring Cloud、Docker的微服务系统架构实践
由于最近公司业务需要,需要搭建基于Spring Cloud的微服务系统.遍访各大搜索引擎,发现国内资料少之又少,也难怪,国内Dubbo正统治着天下.但是,一个技术总有它的瓶颈,Dubbo也有它捉襟见肘 ...
- maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目
项目的简单介绍: 项目采用maven聚合工程 用spring boot 搭建 spring cloud的微服务 模块式开发 项目的截图: 搭建开始: 能上图 我少打字 1.首先搭建maven的聚合工程 ...
- Spring Boot (一): Spring Boot starter自定义
前些日子在公司接触了spring boot和spring cloud,有感于其大大简化了spring的配置过程,十分方便使用者快速构建项目,而且拥有丰富的starter供开发者使用.但是由于其自动化配 ...
随机推荐
- Linux启动报:UNEXPECTED INCONSISTENCY: RUN fsck MANUALLY问题解决
现象: 在此界面输入下root的密码.会进入到修复模式 在修复模式下,输入命令fsck –y /dev/mapper/vg_swnode1-lv_root 这个后面跟的路径就是你上面提示出错的那个路 ...
- Java 之 FileReader FileInputStream InputStreamReader BufferedReader 作用与区别
ava.io下面有两个抽象类:InputStream和ReaderInputStream是表示字节输入流的所有类的超类Reader是用于读取字符流的抽象类InputStream提供的是字节流的读取,而 ...
- postman 带变量参数使用方法
设置代码组变量后就可以在这个组内所有接口使用这些变量了,代码用的是js语法: 代码编写的快捷方法在这里: 然后就可以在接口中使用这些变量了,当然也可以直接在接口中写这些变量
- MongoDB的C#驱动报错Server instance 127.0.0.1:27017 is no longer connected的解决方案
使用C#的MondoDB驱动,一直没问题.结果最近,MongoCursor的ToList方法,取列表,总是报错 Server instance 127.0.0.1:27017 is no longer ...
- 使用SimpleDateFormat时的日期和时间模式
日期和时间模式 日期和时间格式由日期和时间模式 字符串指定.在日期和时间模式字符串中,未加引号的字母 ‘A’ 到’Z’ 和’a’ 到’z’ 被解释为模式字母,用来表示日期或时间字符串元素.文本可以使用 ...
- ThreadPoolExecutor线程池
为什么使用线程池: 1.创建/销毁线程伴随着系统开销,过于频繁的创建/销毁线程,会很大程度上影响处理效率. 2.线程并发数量过多,抢占系统资源从而导致阻塞. 3.对线程进行一些简单的管理. 在java ...
- CSS的再一次深入(更新中···)
全面我们学了6个选择器,今天再来学习两个选择器,分别是通配符选择器和并集选择器: 1.通配符选择器: *{ } 表示body里所有的标签都被选中 2.并集选择器: 选中的标签之间用逗号隔开,表示这几个 ...
- Python3基础 list extend 合并列表
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 【做题】NOWCODER142A Ternary String——数列&欧拉定理
题意:你有一个长度为\(n\),且仅由012构成的字符串.每经过一秒,这个字符串所有1后面会插入一个0,所有2后面会插入一个1,然后会删除第一个元素.求这个字符串需要多少秒变为空串,对\(10^9+7 ...
- Baseline
Baseline Baselines an existing database, excluding all migrations upto and including baselineVersion ...