Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

@CrossOrigin only works when register one domain

I wanted to make an connection between frontend and backend so I used @CrossOrigin annotation Like below.

@CrossOrigin(origins = "http://localhost:3000, http://server ip:3000, http://backend.com:3000")
@RestController
@RequestMapping("/member")
public class MemberController {

    Logger logger = LoggerFactory.getLogger(MemberController.class);
    private MemberService ms;
    private Encryption encrypt;
    private S3FileUploadService sfu;

    @Autowired
    public MemberController(MemberService ms, Encryption encrypt,S3FileUploadService sfu) {
        this.ms = ms;
        this.encrypt = encrypt;
        this.sfu = sfu;
    }

    @GetMapping("/login")
    public FrontMember login(@RequestHeader("Authorization") String autho) {
        logger.info("Authorization : " + autho);

        String memberInform = encrypt.aesDecrypt(autho);
        String[] idPwd = memberInform.split("/");
        Member loginTry = new Member();
        loginTry.setEmail(idPwd[0]);
        loginTry.setPwd(encrypt.shaEncryption(idPwd[1]));
        Member authorizedUser = ms.login(loginTry);
        if(authorizedUser == null){
            logger.warn("No member info");
            return null;
        }else{
            logger.info("Member Get : "+authorizedUser.toString());
            String hashMemberNum = encrypt.aesEncrypt(Integer.toString(authorizedUser.getMemberNum()));
            String mgHash = encrypt.aesEncrypt(Integer.toString(authorizedUser.getMg()));
            FrontMember fm = new FrontMember(hashMemberNum, authorizedUser.getNickName(), authorizedUser.getPfUrl(),mgHash);
            logger.info("Login User : "+fm.toString());
            return fm;
        }
    }
}

But it doesn’t work unless I only put one domain on origin like below.

@CrossOrigin(origins = "http://localhost:3000")

I want to put several domain at crossorigin.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I solve this problem?

>Solution :

Check the Official document of CrossOrign,we can found below description:
enter image description here

So the reason is that you have made a wrong invocation,if you need to allow multiple origins,you need to use an array contains of string instead of single string

In order to make your code work,you can try with below:

@CrossOrigin(origins = {"http://localhost:3000", "http://server ip:3000", "http://backend.com:3000"})
@RestController
@RequestMapping("/member")
public class MemberController {
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading