spring boot集成struts2引用security标签tags

java技术文章

2018-10-28

66

0

最近在学习微服务系统,因为我之前的项目使用到了spring security的tag标签来作为权限控制,所以就想直接移植到现在的系统中,但是在移植的过程中遇到了不少的问题,今天就吧遇到的问题都整理出来。

一、maven包的引用

struts2 maven引入:

<dependency>
		    <groupId>org.apache.struts</groupId>
		    <artifactId>struts2-core</artifactId>
		    <exclusions>
		        <exclusion>
		            <groupId>asm</groupId>
		            <artifactId>asm</artifactId>
		        </exclusion>
		        <exclusion>
		            <groupId>asm</groupId>
		            <artifactId>asm-commons</artifactId>
		        </exclusion>
		    </exclusions>
		</dependency>
		<dependency>
		    <groupId>org.apache.struts</groupId>
		    <artifactId>struts2-spring-plugin</artifactId>
 		    <exclusions> 
 				<exclusion> 
 					<groupId>org.springframework</groupId> 
					<artifactId>spring-beans</artifactId> 
 				</exclusion> 
 				<exclusion> 
 					<groupId>org.springframework</groupId> 
 					<artifactId>spring-core</artifactId> 
 				</exclusion> 
 				<exclusion> 
 					<groupId>org.springframework</groupId> 
					<artifactId>spring-context</artifactId> 
 				</exclusion> 
 				<exclusion> 
					<groupId>org.springframework</groupId> 
 					<artifactId>spring-expression</artifactId> 
   				</exclusion> 
				<exclusion> 
					<groupId>org.springframework</groupId> 
 					<artifactId>spring-aop</artifactId> 
				</exclusion> 
				<exclusion> 
					<groupId>org.springframework</groupId> 
					<artifactId>spring-web</artifactId> 
				</exclusion> 
				<exclusion> 
					<groupId>org.springframework</groupId> 
					<artifactId>spring-webmvc</artifactId> 
				</exclusion> 
				<exclusion> 
 					<groupId>org.springframework</groupId> 
					<artifactId>spring-jcl</artifactId> 
				</exclusion> 
			</exclusions> 
		</dependency>
		<dependency>
		    <groupId>org.apache.struts</groupId>
		    <artifactId>struts2-convention-plugin</artifactId>
		</dependency>
		<dependency>
		    <groupId>org.apache.struts</groupId>
		    <artifactId>struts2-json-plugin</artifactId>
		</dependency>
		<dependency>
		    <groupId>org.apache.commons</groupId>
		    <artifactId>commons-jci-core</artifactId>
		</dependency>

spring maven包引入:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-autoconfigure-processor</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-configuration-metadata</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-devtools</artifactId>
</dependency>		
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-webmvc</artifactId>
  </dependency>
<dependency>
	<groupId>org.springframework.security</groupId>
	<artifactId>spring-security-core</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.security</groupId>
	<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.security</groupId>
	<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.security</groupId>
	<artifactId>spring-security-taglibs</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-framework-bom</artifactId>
	<scope>import</scope>
	<type>pom</type>
</dependency>
<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>jsp-api</artifactId>
</dependency>
<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>servlet-api</artifactId>
</dependency>
<dependency>
	<groupId>org.yaml</groupId>
	<artifactId>snakeyaml</artifactId>
</dependency>
<dependency>
		<groupId>org.jasypt</groupId>
		<artifactId>jasypt</artifactId>
</dependency>
<dependency>
	<groupId>com.thoughtworks.xstream</groupId>
	<artifactId>xstream</artifactId>
</dependency>
<dependency>
	<groupId>org.bouncycastle</groupId>
	<artifactId>bcprov-jdk15on</artifactId>
</dependency>

二、spring security Filter配置

在spring boot入口类中添加下面两个方法,主要是用来添加springSecurityFilterChain和JspSupportServlet:

springSecurityFilterChain:security设置filter。

JspSupportServlet:struts2读取security tag标签设置。

@SuppressWarnings({"rawtypes", "unchecked" })
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        StrutsPrepareAndExecuteFilter struts = new StrutsPrepareAndExecuteFilter();
        registrationBean.setFilter(new DelegatingFilterProxy("springSecurityFilterChain"));
        registrationBean.setFilter(struts);
        registrationBean.setOrder(1);
        return registrationBean;
    }
    
    @SuppressWarnings({"rawtypes", "unchecked" })
    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
         ServletRegistrationBean registrationBean = new ServletRegistrationBean();
         registrationBean.setName("JspSupportServlet");
         registrationBean.setServlet(new JspSupportServlet());
         registrationBean.addUrlMappings("/JspSupportServlet");
         registrationBean.setLoadOnStartup(1);
         return registrationBean;
     }

这里需要注意一点:registrationBean.addUrlMappings("/JspSupportServlet");不添加这句代码的话,如果项目静态资源设置正确的话,会访问不了静态资源,会提示如下错误:

HTTP Status 405 - HTTP method GET is not supported by this URL

三、静态资源设置

spring静态资源设置:

因为我使用的是spring boot application.yml默认的配置文件,所以我的项目只需要在配置文件中添加如下配置:

pring.resources:
  static-locations: /js/,/images/,/css/,classpath:/resources/
spring.mvc:
  servlet.path: 
  static-path-pattern: /**
  view.prefix: 
  view.suffix: .ftl

其中,js、images、css、resources是我的webapp下的文件目录。

static-locations:就是设置将原来的静态资源路径修改成我自己项目的资源路径。

static-patch-pattern: /**就是所有的请求都要是否包含静态资源文件路径。

struts静态资源文件设置:

struts只需要在struts2.xml文件中添加如下配置:

<constant name="struts.action.excludePattern" value="/js/*,/css/*" />

四、引用security tags

我的项目是在项目启动的时候加载该标签,所以我的引用方式如下:

<#global sec=JspTaglibs["http://www.springframework.org/security/tags"] />

另一种引用方式是在JSP或者是ftl文件中直接引用:

<#assign sec=JspTaglibs["http://www.springframework.org/security/tags"]/> 

大概就遇到上面这几个问题,如果后面再遇到其他问题,我会及时和大家分享。

欢迎访问:www.hongfu951.com博客,查看更多文章

发表评论

全部评论:0条

鸿福951

努力打造一个好用的webui

热评文章

推荐文章