• 工作中遇到的问题--实现程序运行时就加载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;

  • 相关阅读:
    一周进度条博客
    4.8地铁查询开发进度
    4.7地铁查询开发进度
    4.6地铁查询系统开发进度
    大二下第六周学习进度报告
    4.5地铁查询系统开发进度2
    《构建之法》阅读笔记03
    石家庄地铁查询系统开发进度
    《构建之法》阅读笔记02
    大二下第五周学习笔记
  • 原文地址:https://www.cnblogs.com/ly-radiata/p/4789678.html
Copyright © 2020-2023  润新知