मैंने उदाहरण के लिए 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;
}
}
WithMockOAuthUser के लिए कोड कहां है और यह संबंधित सुरक्षा सेन्टेक्स्टफैक्टरी से संबंधित है? –
@RobWinch क्षमा करें कि एक टाइपो है, यह वास्तव में WithMockCustomUser है जो मेरे साथ गड़बड़ कर एक टाइपो था, वही समस्या मौजूद है।मेरे पास एक और इंटरफ़ेस था जिसने उपयोगकर्ता नाम पासवर्ड के बजाय ओएथ प्रमाणीकरण बनाया है प्रमाणीकरण – revilo
आपकी सुरक्षा कॉन्फ़िगरेशन कैसा दिखता है? विशेष रूप से वेब और विधि सुरक्षा कॉन्फ़िगरेशन कैसा दिखता है? –