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

  • 相关阅读:
    SQL SERVER 2008 SA禁用,Windows帐户被删
    SQL Server表结构修改脚本
    请教高手,如何取得Target属性
    Installing Reporting Services on Windows 7, Vista or Windows Server 2008 无权限(rsAccessDenied)解决方法
    请教:如何在子页面关闭时把焦点设置到父页面的服务器控件上?
    order by newid() sql随机查询
    JS干货,笔记大全
    破解XP密码
    直接连接*.mdf 文件 获取随机数据
    总结C#获取当前路径的7种方法(转)
  • 原文地址:https://www.cnblogs.com/ly-radiata/p/4789678.html
Copyright © 2020-2023  润新知