5

मैंने उदाहरण के लिए OAuth2 @PreAuthorise (हैएनीरोल ('एडमिन', 'टेस्ट') का प्रयास करने और परीक्षण करने के लिए निम्न लिंक का पालन किया है, लेकिन मैं पास करने या यहां तक ​​कि प्रमाणित करने के लिए कोई भी परीक्षण नहीं कर सकता।स्प्रिंग सिक्योरिटी विद सिक्योरिटी कॉन्टेक्स्ट मॉकएमवीसी ओएथ 2 हमेशा अनधिकृत

जब मैं व्यवस्थापक (या किसी भी भूमिका के साथ) के अंत बिंदु तक पहुंचने का प्रयास करता हूं तो यह कभी भी सही तरीके से प्रमाणीकृत नहीं होगा। क्या मुझे कुछ स्पष्ट याद आ रही है, ऐसा लगता है कि मेरे पास उदाहरणों में सब कुछ है। मैंने एक और विकल्प भी कोशिश की है OAuth विशिष्ट प्रमाणीकरण के साथ WithSecurityContext फैक्टरी के लिए और अभी भी कोई भाग्य नहीं है। किसी भी मदद की सराहना की जाएगी।

https://stackoverflow.com/a/31679649/2594130 और http://docs.spring.io/spring-security/site/docs/4.0.x/reference/htmlsingle/#test

मेरे नियंत्रक मैं परीक्षण कर रहा हूँ

@RestController 
@RequestMapping("/bookmark/") 
public class GroupBookmarkController { 

    @Autowired 
    BookmarkService bookmarkService; 

    /** 
    * Get list of all bookmarks 
    */ 
    @RequestMapping(value = "{groupId}", method = RequestMethod.GET) 
    @PreAuthorize("hasAnyRole(['ADMIN', 'USER'])") 
    public ResponseEntity<List<Bookmark>> listAllGroupBookmarks(@PathVariable("groupId") String groupId) throws BookmarkNotFoundException { 
     List<Bookmark> bookmarks = bookmarkService.findAllBookmarksByGroupId(groupId); 
     return new ResponseEntity<>(bookmarks, HttpStatus.OK); 
    } 
    ... 
} 

मेरे टेस्ट वर्ग

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(classes = BookmarkServiceApplication.class) 
@WebAppConfiguration 
public class BookmarkServiceApplicationTests { 

    private MockMvc mockMvc; 

    @Autowired 
    private WebApplicationContext webApplicationContext; 

    @Before 
    public void loadData() { 
     this.mockMvc = MockMvcBuilders 
       .webAppContextSetup(webApplicationContext) 
       .apply(springSecurity()) 
       .alwaysDo(print()) 
       .build(); 
    } 

    @Test 
    @WithMockCustomUser(username = "test") 
    public void getBookmarkAuthorised() throws Exception { 
     mockMvc.perform(get("/bookmark/nvjdbngkjlsdfngkjlfdsnlkgsd")) 
       .andExpect(status().is(HttpStatus.SC_OK)); 
     // always 401 here 
    } 
} 

मेरे BookmarkServiceApplication

@SpringBootApplication 
@EnableResourceServer 
public class BookmarkServiceApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(BookmarkServiceApplication.class, args); 
    } 
} 

मेरे WithSecurityContextFactory

public class WithMockCustomUserSecurityContextFactory implements WithSecurityContextFactory<WithMockCustomUser> { 
    @Override 
    public SecurityContext createSecurityContext(WithMockCustomUser customUser) { 
     SecurityContext context = SecurityContextHolder.createEmptyContext(); 

     List<GrantedAuthority> grantedAuthorities = new ArrayList<>(); 
     grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_ADMIN")); 

     UserDetails principal = new User(customUser.username(), "password", true, true, true, true, grantedAuthorities); 


     Authentication authentication = new UsernamePasswordAuthenticationToken(
       principal, principal.getPassword(), principal.getAuthorities()); 
     context.setAuthentication(authentication); 

     return context; 
    } 
} 

मेरे WithSecurityContext एनोटेशन

@Retention(RetentionPolicy.RUNTIME) 
@WithSecurityContext(factory = WithMockCustomUserSecurityContextFactory.class) 
public @interface WithMockCustomUser { 

    String username() default "user"; 

    String name() default "Test User"; 
} 

प्रति @RobWinch जबाब के

हाय @RobWinch जैसा कि मैंने राज्यविहीन ध्वज के साथ आप सुझाव की कोशिश की है, इस सवाल का जवाब का हिस्सा साथ मदद की। हालांकि इस सवाल का जवाब देते समय [स्प्रिंग OAuth और बूट एकता टेस्ट] (https://stackoverflow.com/a/31679649/2594130) आप का उल्लेख

अब आप ऐसा क्यों है कि मैं है या नहीं राज्यविहीन मोड में चल रहा

के बारे में चिंता करने की जरूरत अभी भी स्टेटलेस झूठी जोड़ने की जरूरत है, क्या यह एक बग है या क्या हम इसे थोड़ा अलग तरीके से उपयोग कर रहे हैं?

दूसरी बात मैं काम करने के लिए इस WithSecurityContextFactory को OAuth2Request और OAuth2Authentication जोड़ने था प्राप्त करने के लिए के रूप में आप निम्नलिखित

public class WithMockCustomUserSecurityContextFactory implements WithSecurityContextFactory<WithMockOAuthUser> { 

    @Override 
    public SecurityContext createSecurityContext(WithMockOAuthUser withClient) { 
     // Get the username 
     String username = withClient.username(); 
     if (username == null) { 
      throw new IllegalArgumentException("Username cannot be null"); 
     } 

     // Get the user roles 
     List<GrantedAuthority> authorities = new ArrayList<>(); 
     for (String role : withClient.roles()) { 
      if (role.startsWith("ROLE_")) { 
       throw new IllegalArgumentException("roles cannot start with ROLE_ Got " + role); 
      } 
      authorities.add(new SimpleGrantedAuthority("ROLE_" + role)); 
     } 

     // Get the client id 
     String clientId = withClient.clientId(); 
     // get the oauth scopes 
     String[] scopes = withClient.scope(); 
     Set<String> scopeCollection = Sets.newSet(scopes); 

     // Create the UsernamePasswordAuthenticationToken 
     User principal = new User(username, withClient.password(), true, true, true, true, authorities); 
     Authentication authentication = new UsernamePasswordAuthenticationToken(principal, principal.getPassword(), 
       principal.getAuthorities()); 


     // Create the authorization request and OAuth2Authentication object 
     OAuth2Request authRequest = new OAuth2Request(null, clientId, null, true, scopeCollection, null, null, null, 
       null); 
     OAuth2Authentication oAuth = new OAuth2Authentication(authRequest, authentication); 

     // Add the OAuth2Authentication object to the security context 
     SecurityContext context = SecurityContextHolder.createEmptyContext(); 
     context.setAuthentication(oAuth); 
     return context; 
    } 

} 
+0

WithMockOAuthUser के लिए कोड कहां है और यह संबंधित सुरक्षा सेन्टेक्स्टफैक्टरी से संबंधित है? –

+0

@RobWinch क्षमा करें कि एक टाइपो है, यह वास्तव में WithMockCustomUser है जो मेरे साथ गड़बड़ कर एक टाइपो था, वही समस्या मौजूद है।मेरे पास एक और इंटरफ़ेस था जिसने उपयोगकर्ता नाम पासवर्ड के बजाय ओएथ प्रमाणीकरण बनाया है प्रमाणीकरण – revilo

+0

आपकी सुरक्षा कॉन्फ़िगरेशन कैसा दिखता है? विशेष रूप से वेब और विधि सुरक्षा कॉन्फ़िगरेशन कैसा दिखता है? –

उत्तर

2

समस्या में देख सकते हैं करने के लिए की जरूरत है कि OAuth2AuthenticationProcessingFilter SecurityContext साफ हो जाएगा अगर यह होता है है स्टेटलेस के रूप में चिह्नित इसे हल करने के लिए इसे राज्य को बाहरी रूप से पॉप्युलेट करने की अनुमति देने के लिए कॉन्फ़िगर करें (यानी stateless = false)।

+0

हाय @ रोबविच मैंने आपको स्टेटलेस फ्लैग के साथ सुझाव देने की कोशिश की है, इससे उत्तर के हिस्से में मदद मिली है। हालांकि इस प्रश्न के उत्तर में [स्प्रिंग ओथ और बूट एकीकरण टेस्ट] (http://stackoverflow.com/a/31679649/2594130) आप उल्लेख करते हैं "अब आपको स्टेटलेस मोड में चलने की चिंता करने की आवश्यकता नहीं है या नहीं" _ क्यों क्या यह आवश्यक है कि मुझे इसकी आवश्यकता है, क्या यह एक बग है या हम इसे थोड़ा अलग तरीके से उपयोग कर रहे हैं। मैंने उत्तर अपडेट किया है क्योंकि मैंने यहां वर्णों से बाहर चलाया है – revilo

+0

स्प्रिंग सिक्योरिटी ओएथ स्प्रिंग सिक्योरिटी की तुलना में अलग-अलग स्टेटलेस को संभालता है और इसलिए यह कथन OAuth (दुर्भाग्य से) के लिए लागू नहीं है। –

+0

वास्तव में आप उपरोक्त उदाहरण में उस = झूठी कैसे सेट करते हैं? – PaulNUK

1

कैसे गलत पर स्टेटलेस स्थापित करने के लिए कुछ और अधिक infos जोड़ने के लिए:

अपने ResourceServerConfigurerAdapter में निम्नलिखित है:

@Override 
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 
     resources.stateless(false); 
    } 

जो मेरे लिए काम किया।

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