zoukankan      html  css  js  c++  java
  • 工作中遇到的问题--实现程序运行时就加载CustomerSetting的第二种方法

    写一个自定义注解

    @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Qualifier
    public @interface CurrentCustomerSettings {

    }

    在web初始化类中添加:

    @Configuration
    @Order(3)
    @EnableWebMvcSecurity
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    public class MFGWebSecurityConfigurerAdapter extends
            AWebSecurityConfigurerAdapter {

        @Autowired
        private UserRepository userRepository;

        @Autowired
        private CustomerSettingsRepository customerSettingsRepository;

        @Override
        protected void configure(HttpSecurity httpSecurity) throws Exception {
            httpSecurity
                    .formLogin()
                    .successHandler(
                            new SavedRequestAwareAuthenticationSuccessHandler())
                    .loginPage("/login").permitAll().failureUrl("/login-error")
                    .defaultSuccessUrl("/").and().logout()
                    .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                    .logoutSuccessUrl("/logged-out").permitAll().and().rememberMe()
                    .key(SECURITY_TOKEN)
                    .tokenRepository(persistentTokenRepository())
                    .tokenValiditySeconds(mfgSettings.getRememberMeTokenValidity())
                    .and().sessionManagement().maximumSessions(1)
                    .sessionRegistry(sessionRegistry).and().sessionFixation()
                    .migrateSession().and().authorizeRequests().anyRequest()
                    .authenticated();
        }

        @Bean
        public PersistentTokenRepository persistentTokenRepository() {
            JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
            tokenRepository.setDataSource(dataSource);
            return tokenRepository;
        }

        @Bean
        @LoggedInUser
        @Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
        @Transactional(readOnly = true)
        public User getLoggedInUser() {
            Authentication authentication = SecurityContextHolder.getContext()
                    .getAuthentication();
            if (authentication != null
                    && !(authentication instanceof AnonymousAuthenticationToken)
                    && authentication.isAuthenticated())
                return userRepository.findByLogin(authentication.getName());
            return null;
        }

        @Bean
        @SystemUser
        @Scope(value = WebApplicationContext.SCOPE_APPLICATION, proxyMode = ScopedProxyMode.NO)
        @Transactional(readOnly = true)
        public User getSystemUser() {
            return userRepository.findByLogin(Constants.SYSTEM_USER);
        }

        @Bean

        @CurrentCustomerSettings
        @Scope(value = WebApplicationContext.SCOPE_APPLICATION, proxyMode = ScopedProxyMode.NO)
        public CustomerSettings customerSettings() {
            return customerSettingsRepository.findAll().get(0);
        }

    以后在注入的时候,只需要写:

    @CurrentCustomerSettings

    @Autowired

    CustomerSettings customerSettings;

  • 相关阅读:
    如何打印调试字符串?
    如何测试代码执行耗时?
    access 如何导出 cvs 文件?
    opencv如何打印长图?
    window 注册表上下文菜单如何配置?
    python 零散记录(四) 强调字典中的键值唯一性 字典的一些常用方法
    python 零散记录(三) 格式化字符串 字符串相关方法
    python 零散记录(二) 序列的相关操作 相加 相乘 改变 复制
    python 零散记录(一) input与raw_input 数学相关函数 转换字符串的方法
    devi into python 笔记(七)locals与globals 字典格式化字符串 字符集
  • 原文地址:https://www.cnblogs.com/ly-radiata/p/4789678.html
Copyright © 2011-2022 走看看