Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
- String username="tt";
- if (principal instanceof UserDetails) {
- username = ((UserDetails)principal).getUsername();
- } else {
- username = principal.toString();
- }
- return username;
- @Configuration
- /**In Memory Authentication
- * @param authenticationManagerBuilder Allow to build an AuthenticationManager
- */
- @Bean
- public UserDetailsService mongoUserDetails() {
- return new UserService();
- }
- @Autowired
- public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) {
- try {
- UserDetailsService userDetailsService = mongoUserDetails();
- authenticationManagerBuilder.userDetailsService(userDetailsService);
- } catch (Exception exception) {
- LOG.error("In Memory Authentication has failed", exception);
- }
- }
- @Service
- @Override
- public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
- MongoCollection<Document> collection = MongoConnection.getConnection().getCollection("users");
- Document document = collection.find(Filters.eq("username",username)).first();
- if(document!=null) {
- String username1 = document.getString("username");
- String password = document.getString("password");
- List<String> authorities = (List<String>) document.get("authorities");
- MongoUserDetails mongoUserDetails = new MongoUserDetails(username1,password,authorities.toArray(new String[authorities.size()]));
- return mongoUserDetails;
- }
- return null;
- }
Add Comment
Please, Sign In to add comment