2017-05-16 8 views
9

के लिए अलग-अलग लॉगिन हैलो मैं एक लॉगिन पृष्ठ के साथ स्प्रिंग सुरक्षा के साथ सुरक्षित एक वेब एप्लिकेशन है। यह मेरी सुरक्षा विन्यासस्प्रिंग ओयूथ - वेब और आरईएसटी

@Configuration 
@ComponentScan("it.besmart") 
@EnableWebSecurity 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{ 

    @Autowired 
    @Qualifier("customUserDetailsService") 
    UserDetailsService userDetailsService; 

    @Autowired 
    CustomSuccessHandler customSuccessHandler; 

    @Autowired 
    CustomAuthenticationFailureHandler customAuthenticationFailureHandler; 

    @Autowired 
    DataSource dataSource; 

    @Autowired 
    private ConnectionFactoryLocator connectionFactoryLocator; 

    @Autowired 
    private UsersConnectionRepository usersConnectionRepository; 

    @Autowired 
    private FacebookConnectionSignup facebookConnectionSignup; 



    private final static Logger logger = LoggerFactory.getLogger(SecurityConfiguration.class); 

    @Autowired 
    public void configureGlobalService(AuthenticationManagerBuilder auth) throws Exception { 
     auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); 

    } 

    @Bean 
    public PasswordEncoder passwordEncoder() { 
     return new BCryptPasswordEncoder(); 
    } 


     protected void configure(HttpSecurity http) throws Exception { 
      logger.debug("Webapp security configured"); 
      http. 

      authorizeRequests() 

        .antMatchers("/", "/register", "/registrationConfirm", "/resendRegistrationToken", "/park/**") 
        .permitAll() 

        .antMatchers("/edit/**", "/payment/**", "/plate/**", "/book/**", "/home", "/stop/**", 
          "/notification/**", "/include/**") 
        .access("hasRole('USER') or hasRole('ADMIN') or hasRole('PARK')").antMatchers("/admin/**") 
        .access("hasRole('ADMIN') or hasRole('PARK')").antMatchers("/updatePassword") 
        .hasAuthority("CHANGE_PASSWORD_PRIVILEGE") 

        .and().formLogin().loginPage("/") 
        .successHandler(customSuccessHandler).failureHandler(customAuthenticationFailureHandler) 
        .usernameParameter("email").passwordParameter("password").and().rememberMe() 
        .rememberMeParameter("remember-me").tokenRepository(persistentTokenRepository()) 
        .tokenValiditySeconds(86400).and().exceptionHandling().accessDeniedPage("/Access_Denied").and() 
        .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")) 
        .logoutSuccessUrl("/?logout=true").permitAll(); 
     } 


     @Override 
     @Bean 
     public AuthenticationManager authenticationManagerBean() throws Exception { 
      return super.authenticationManagerBean(); 
     } 

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

} 

यह मेरी सभी वेब आवेदन प्राप्त करके अच्छा काम करता है।

उसी एप्लिकेशन में मेरे पास कुछ आरईएसटी एपीआई की सुरक्षा के लिए संसाधन/प्राधिकरण सर्वर भी है।

कुछ संसाधन प्राधिकरण कोड अनुदान से सुरक्षित हैं, इसलिए अविश्वसनीय मोबाइल ऐप को मेरे आवेदन से लॉगिन फॉर्म के साथ एक्सेस टोकन लेना चाहिए। मैं चाहता हूं कि एप्लिकेशन मोबाइल ऐप से लॉगिन करने का प्रयास करते समय एक अलग लॉगिन पेज का उपयोग करें।

यह मेरा resourceServer विन्यास

@EnableResourceServer 
@ComponentScan("it.besmart.easyparking") 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class ResourceServerConfig { 

    private final Logger logger = LoggerFactory.getLogger(ResourceServerConfig.class); 

    @Autowired 
    DataSource dataSource; 

    private static final String RESOURCE_ID = "easyparking_api"; 

    @Configuration 
    // @Order(2) 
    public class grantCredentialsConfiguration extends ResourceServerConfigurerAdapter { 
     @Override 
     public void configure(HttpSecurity http) throws Exception { 
      logger.debug("Api security configured"); 

      http 

        .requestMatchers().antMatchers("/api/oauth/**").and().authorizeRequests() 
        .antMatchers("/api/oauth/**").access("hasRole('USER')").and().formLogin().loginPage("/apilogin") 
        .permitAll(); 
     } 

     @Override 
     public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 

      resources.tokenStore(tokenStore()).resourceId(RESOURCE_ID); 
     } 
    } 

    @Configuration 
    // @Order(4) 
    public class clientCredentialsConfiguration extends ResourceServerConfigurerAdapter { 
     @Override 
     public void configure(HttpSecurity http) throws Exception { 
      logger.debug("Client security configured"); 
      http 
        .requestMatchers().antMatchers("/oauth2/**", "/api/registration", "/api/park/**").and() 
        .authorizeRequests().antMatchers("/oauth2/**", "/api/registration", "/api/park/**").authenticated(); 
     } 

     @Override 
     public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 

      resources.tokenStore(tokenStore()).resourceId(RESOURCE_ID); 
     } 
    } 

    @Bean 
    public TokenStore tokenStore() { 
     return new JdbcTokenStore(dataSource); 
    } 

} 

हां, तो grantCredentialsConfiguration अनुरोध रीडायरेक्ट करना चाहिए करने के लिए/apilogin रूप है, लेकिन ऐसा नहीं, मैं मुख्य वेब अनुप्रयोग लॉगिन पृष्ठ पर पुनः निर्देशित कर रहा हूँ करता है ... यह कैसे हो सकता है पूरा किया?

संपादित

लॉग में करीब देख रहे हैं, यह है कि तरह लग रहा है जब मैं प्राप्त करने का प्रयास/OAuth/अधिकृत करते हैं/सामान्य सुरक्षा श्रृंखला जगह लेता है और मैं

2017-05-25 12:23:15 DEBUG o.s.security.web.FilterChainProxy[310] - /oauth/authorize?response_type=token&client_id=test&redirect_uri=https://www.getpostman.com/oauth2/callback reached end of additional filter chain; proceeding with original chain 
2017-05-25 12:23:15 DEBUG o.s.s.o.p.e.FrameworkEndpointHandlerMapping[310] - Looking up handler method for path /oauth/authorize 
2017-05-25 12:23:15 DEBUG o.s.s.o.p.e.FrameworkEndpointHandlerMapping[317] - Returning handler method [public org.springframework.web.servlet.ModelAndView org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint.authorize(java.util.Map<java.lang.String, java.lang.Object>,java.util.Map<java.lang.String, java.lang.String>,org.springframework.web.bind.support.SessionStatus,java.security.Principal)] 
2017-05-25 12:23:15 DEBUG o.s.s.w.a.ExceptionTranslationFilter[163] - Authentication exception occurred; redirecting to authentication entry point 
org.springframework.security.authentication.InsufficientAuthenticationException: User must be authenticated with Spring Security before authorization can be completed. 

तो यह लग रहा है मिल अनुरोध के प्रबंधन के लिए एक हैंडलर की तलाश करना, वांछित के रूप में/एपीआई/एपिलोगिन पर रीडायरेक्ट करने की बजाय, उसे Authentication exception मिल जाता है और इसलिए मैं मानक लॉगिन पेज पर जाता हूं ... लेकिन मुझे यह अपवाद क्यों मिलता है? के बाद से यह अप्रमाणित पहुंच सूची में नहीं जोड़ा गया है

उत्तर

0

, आप URL पथ /apilogin जोड़ने की कोशिश की

.antMatchers("/", "/register", "/registrationConfirm",/resendRegistrationToken", "/park/**") 
.permitAll() 

लिए मैं आवेदन आम प्रमाणीकरण प्रवेश पृष्ठ पर/apilogin पहुँच रीडायरेक्ट हो रहा है अनुमान लगा रहा है।

+0

धन्यवाद उत्तर के लिए, मैं करने की कोशिश की है, लेकिन मैं वेब अनुप्रयोग लॉगिन पृष्ठ पर पुनः निर्देशित किया जाना जारी, apilogin खाते में – besmart

0

ऐसा इसलिए हो रहा है क्योंकि आपने सुरक्षा कॉन्फ़िगरेशन कक्षाओं का ऑर्डर निर्दिष्ट नहीं किया है।

वसंत सुरक्षा संसाधनों में संरक्षण सामान्य से सामान्य से उल्लेख किया जाना चाहिए।

कक्षा SecurityConfigurationgrantCredentialsConfiguration से अधिक सामान्य है। चूंकि दोनों संसाधनों का पालन करते हैं।

  • SecurityConfiguration की सुरक्षा करता /** (Default URL)
  • grantCredentialsConfiguration /api/oauth/**

के बाद से आदेश परिभाषित नहीं है, SecurityConfiguration के सामान्य विन्यास grantCredentialsConfiguration

द्वारा विशिष्ट विन्यास को छुपाता है काम करने के लिए इन प्राप्त करने के लिए जैसा कि अपेक्षित है आपको नीचे दिए गए आदेश को परिभाषित करना होगा।

@Configuration 
@Order(2)//Generic config should have larger value (lower priority) 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{ 
} 

@Configuration 
@Order(1)//Specific with lower value (higher priority) 
public class grantCredentialsConfiguration extends ResourceServerConfigurerAdapter { 

} 

नोट: चूंकि इन प्रवेश पृष्ठों विभिन्न अनुप्रयोगों से नहीं हैं, वे SecurityContextHolder या सुरक्षा संदर्भ साझा करें। इसलिए यदि आप एक लॉगिन पृष्ठ से लॉगिन करते हैं और फिर दूसरे के संरक्षित संसाधन पर जाने का प्रयास करते हैं, तो आपको अगले लॉगिन पृष्ठ पर रीडायरेक्ट नहीं किया जाएगा। इसके बजाय आपको 403 मिलेगा (विभिन्न लॉगिन पृष्ठों द्वारा नियुक्त भूमिकाओं के आधार पर)। एक समय में केवल एक लॉगिन सत्र बनाए रखा जा सकता है।

यहाँ Github पर एक नमूना

https://github.com/ConsciousObserver/TestMultipleLoginPages.git

+0

स्पष्टीकरण के लिए धन्यवाद लिया कभी नहीं किया गया है , मैंने अपने कॉन्फ़िगरेशन के ऑर्डर सेट किए हैं, लेकिन मुझे अभी भी मुख्य लॉगिन पेज पर रीडायरेक्ट किया गया है जब मैं/api/oauth संसाधनों तक पहुंचने का प्रयास कर रहा हूं ... – besmart

+0

मैंने जो नमूना जिथब प्रोजेक्ट लिंक किया है, उसे मतभेदों की तुलना करें। – 11thdimension

+0

ने कोशिश की, नमूना काम करता है, मेरा नहीं ... मुझे लगता है कि अंतर यह है क्योंकि नमूना दो 'WebSecurityConfigurerAdapter' का उपयोग करता है, जबकि मैं एक' WebSecurityConfigurerAdapter' का उपयोग करने की कोशिश कर रहा हूं और एक 'संसाधन सर्वर कॉन्फ़िगरर एडाप्टर' – besmart

संबंधित मुद्दे