`
lsh2366254
  • 浏览: 39046 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

springMVC+spring 切换springBoot2 注意事项

阅读更多

1.web.xml 迁移

   filter迁移

     /**

  * 注册filter
  *
  * @return
  */
@Bean
 public FilterRegistrationBean filterRegistrationBean() {
     FilterRegistrationBean authenticationFilterbean = new FilterRegistrationBean(new AuthenticationFilter());
     Map<String, String> authenticationFilterInitPara = new HashMap<String, String>();
     authenticationFilterInitPara.put("authUrlPattern", "/auth");
     authenticationFilterInitPara.put("authStatusUrlPattern", "/authStatus");
     authenticationFilterInitPara.put("popupLoginSuccessUrlPattern", "/popupLoginSuccess");
     authenticationFilterbean.setInitParameters(authenticationFilterInitPara);
     authenticationFilterbean.addUrlPatterns("/*");
     return authenticationFilterbean;}

 servlet注册

  @Bean

public ServletRegistrationBean AuthservletRegistrationBean() {
    ServletRegistrationBean authServlet = new ServletRegistrationBean(new AuthenticationServlet(), "/auth");
     authServlet.setLoadOnStartup(2);
    return authServlet;
}
listener注册
@Bean
public ServletListenerRegistrationBean ProviderContextListenerRegistrationBean() {
    ServletListenerRegistrationBean listenerRegistrationBean = new ServletListenerRegistrationBean(new ProviderContextListener());
    return listenerRegistrationBean;
}

    context-param初始化参数

    new ServletContextInitializer() {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        servletContext.setInitParameter("auth-timeout-seconds", "3600");
        servletContext.setInitParameter("auth-code-timeout-seconds", "60");
        servletContext.setInitParameter("request-params-snapshot-timeout-seconds", "300");
        servletContext.setInitParameter("webAppRootKey", "slvcms-admin");
    }
}

    error页面配置

    @Component

@Slf4j
public class ErrorPageConfig implements ErrorPageRegistrar {
    @Override
    public void registerErrorPages(ErrorPageRegistry registry) {
        ErrorPage e404=new ErrorPage(HttpStatus.NOT_FOUND,"/404.jsp");
        ErrorPage e500=new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,"/404.jsp");
        registry.addErrorPages(e404,e500);
    }
}

拦截器和默认首页配置
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
public void addInterceptors(InterceptorRegistry registry) {
        //注册LoginHandlerInterceptor拦截器
InterceptorRegistration registration = registry.addInterceptor(new LoginHandlerInterceptor());
        //所有路径都被拦截
registration.addPathPatterns("/**");
        InterceptorRegistration httpOnlyInterceptor = registry.addInterceptor(new HttpOnlyInterceptor());
        //所有路径都被拦截
httpOnlyInterceptor.addPathPatterns("/**");
    }

    /**
     * 默认首页设置,当请求时项目地址的时候 返回login
     */
@Override
public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:index.jsp");
    }
}
2.jboss容器需增加配置jboss-deployment-structure.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--主要是为了使用应用自己的日志系统 -->
<jboss-deployment-structure>
   <deployment>
      <exclusions>
         <module name="org.apache.log4j"/>
         <module name="org.slf4j" />
         <module name="org.slf4j.impl" />
         <module name="org.slf4j.jcl-over-slf4j" />
         <module name="org.slf4j.ext" />
         <module name="org.jboss.as.jsf" />
         <module name="org.jboss.as.jsf-injection" />
         <module name="com.fasterxml.jackson.core.jackson-core" />
         <module name="com.fasterxml.jackson.core.jackson-annotations" />
         <module name="com.fasterxml.jackson.core.jackson-databind" />
         <module name="com.fasterxml.jackson.datatype.jackson-datatype-jdk8" />
         <module name="com.fasterxml.jackson.datatype.jackson-datatype-jsr310" />
         <module name="com.fasterxml.jackson.jaxrs.jackson-jaxrs-json-provider" />
         <module name="org.jboss.resteasy.resteasy-jackson2-provider" />
         <module name="org.jboss.resteasy.resteasy-jackson-provider" />
         <module name="javax.faces.api" slot="main"/>
         <module name="com.sun.jsf-impl" slot="main"/>
      </exclusions>
   </deployment>
</jboss-deployment-structure>
3.shiro权限配置迁移
新建ShiroConfig类
package com.fh.config;

import com.fh.filter.JwtFilter;
import com.fh.interceptor.shiro.MyCredentialsMatcher;
import com.fh.interceptor.shiro.ShiroRealm;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author 用户名
 * @Type ShiroConfig.java
 * @Desc
* @date 2021/2/20 10:11
 */

@Configuration
public class ShiroConfig {
    private static final Logger logger = LoggerFactory
            .getLogger(ShiroConfig.class);

    /**
     * ShiroFilterFactoryBean 处理拦截资源文件过滤器
     * </br>1,配置shiro安全管理器接口securityManage;
     * </br>2,shiro 连接约束配置filterChainDefinitions;
     */
@Bean
public ShiroFilterFactoryBean shiroFilterFactoryBean(
            org.apache.shiro.mgt.SecurityManager securityManager) {
        //shiroFilterFactoryBean对象
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        // 配置shiro安全管理器 SecurityManager
shiroFilterFactoryBean.setSecurityManager(securityManager);
        // 指定要求登录时的链接
shiroFilterFactoryBean.setLoginUrl("/");
        // 登录成功后要跳转的链接
shiroFilterFactoryBean.setSuccessUrl("/main/index");
        // 未授权时跳转的界面;
shiroFilterFactoryBean.setUnauthorizedUrl("/login_toLogin");
        // filterChainDefinitions拦截器
Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        // 配置不会被拦截的链接 从上向下顺序判断
filterChainDefinitionMap.put("/websocket/**", "anon");
        filterChainDefinitionMap.put("/users/**", "anon");
        filterChainDefinitionMap.put("/auth", "anon");
        filterChainDefinitionMap.put("/authStatus", "anon");
        filterChainDefinitionMap.put("/jobDispatcher", "anon");
        // 添加自己的过滤器并且取名为jwt
Map<String, Filter> filterMap = new HashMap<String, Filter>(1);
        filterMap.put("jwt", new JwtFilter());
        filterChainDefinitionMap.put("/**", "jwt");
        shiroFilterFactoryBean.setFilters(filterMap);
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        logger.debug("Shiro拦截器工厂类注入成功");
        return shiroFilterFactoryBean;
    }

    /**
     * shiro安全管理器设置realm认证
     *
     * @return
*/
@Bean("securityManager")
    public DefaultWebSecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        // 设置realm.
securityManager.setRealm(shiroRealm());
        //注入ehcache缓存管理器;
securityManager.setCacheManager(ehCacheManager());
        return securityManager;
    }

    /**
     * 身份认证realm; (账号密码校验;权限等)
     *
     * @return
*/
@Bean
public ShiroRealm shiroRealm() {
        ShiroRealm shiroRealm = new ShiroRealm();
        shiroRealm.setCredentialsMatcher(new MyCredentialsMatcher());
        return shiroRealm;
    }

    /**
     * ehcache缓存管理器;shiro整合ehcache:
     * 通过安全管理器:securityManager
     *
     * @return EhCacheManager
     */
@Bean
public EhCacheManager ehCacheManager() {
        logger.debug("=====shiro整合ehcache缓存:ShiroConfiguration.getEhCacheManager()");
        EhCacheManager cacheManager = new EhCacheManager();
        cacheManager.setCacheManagerConfigFile("classpath:ehcache.xml");
        return cacheManager;
    }

    /**
     * 下面的代码是添加注解支持
     * @return
*/
@Bean
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
        return defaultAdvisorAutoProxyCreator;
    }

    @Bean
public static LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics