成员不能通过实例引用进行访问;用类型名称代替

时间:2019-09-25 09:56:43

标签: c# asp.net-mvc

寻找了很多类似问题的话题后,很抱歉,但是当我看到错误消息时,我仍然不明白我的期望:“ Member ClassMainController.ConnectKBOnline无法使用实例引用进行访问;可以使用类型名称来代替”。

这是我的代码

@RestController
@RequestMapping("/secure_api_v1/accounts/sign_in")
public class LoginController {

    private final AuthenticationManager authenticationManager;

    private final UserRepo userRepo;
    private final PasswordEncoder passwordEncoder;
    private final CustomUserDetailsService customUserDetailsService;

    public LoginController(CustomUserDetailsService customUserDetailsService, AuthenticationManager authenticationManager, PasswordEncoder passwordEncoder, UserRepo userRepo) {
        this.customUserDetailsService = customUserDetailsService;
        this.authenticationManager = authenticationManager;
        this.passwordEncoder = passwordEncoder;
        this.userRepo = userRepo;
    }

    @PostMapping
    public ResponseEntity<JsonResponse> authenticate(AuthentificationRequest authenticationRequest, HttpServletResponse response, HttpServletRequest request) {
        try {
            String username = authenticationRequest.getEmail();
            String password = authenticationRequest.getPassword();

            GenerateToken tokenGenerator = new GenerateToken(passwordEncoder, customUserDetailsService);
            String token = tokenGenerator.getToken(username, password);
            TokenAuthentication authentication = new TokenAuthentication(token);
            SecurityContextHolder.getContext().setAuthentication(authentication);
            Cookie cookie = new Cookie("csrftoken", token);
            cookie.setMaxAge(7 * 24 * 60 * 60);
            cookie.setHttpOnly(true);
            cookie.setPath("/");
//            cookie.setSecure(true);
            response.addCookie(cookie);
            UserDetails userDetails = this.customUserDetailsService.loadUserByUsername(username);
            User user = (User) userDetails;
            user.setToken(token);
            userRepo.save(user);
            List<String> roles = new ArrayList<String>();

            for (GrantedAuthority authority : userDetails.getAuthorities()) {
                roles.add(authority.toString());
            }

            return new ResponseEntity<>(new JsonResponse(true, "Пользователь успешно вошел"), HttpStatus.OK);

        } catch (BadCredentialsException bce) {
            return new ResponseEntity<>(new JsonResponse(false, "Пользователь не найден"), HttpStatus.UNPROCESSABLE_ENTITY);

        } catch (Exception e) {
            return new ResponseEntity<>(new JsonResponse(false, "Ошибка"),HttpStatus.EXPECTATION_FAILED);
        }

    }

}

如果有帮助,我在这里得到了类定义:

    using some_API;

    private static ClassMainController MainController;

    private static void InitMainController()
    {
        if (MainController == null)
        {
            // -- Initialize MainController
            MainController = new ClassMainController();
            MainController.ConnectKBOnline = false;    //Get knowledge base data from files
            MainController.ConnectDBOnline = false;    //Get database data from files
        }
    }

0 个答案:

没有答案