小编Iri*_*ram的帖子

使用Java和Angular2登录Spring Security

我在我的Web应用程序中集成spring security(即登录部分)时遇到了一些麻烦.我的BackEnd在localhost:8080上运行,FrontEnd(Ionic 2与AngularJS 2)在localhost:8100上运行.我设法登录(至少我是这么认为),但其他请求变得不可用,我在Chrome开发者控制台中收到以下错误:

XMLHttpRequest cannot load http://localhost:8080/company/getAll. Response for preflight is invalid (redirect)
Run Code Online (Sandbox Code Playgroud)

当使用Postman进行测试时,它似乎正在工作,我登录然后我能够在http:// localhost:8080/company/getAll上执行POST请求,一切都按预期工作.

据推测,我错过了某些东西(比如令牌)但却无法弄清楚是什么.我是Ionic和Spring Security的新手,所以请原谅我,如果这是微不足道的话.我尝试使用谷歌搜索各种教程,但无法找到任何东西(大多数都使用JSP).

我怎么能让这个工作?我的前端请求应该怎么样?

这是我从BackEnd控制器登录的方法:

@RequestMapping(value = "/login", method = RequestMethod.GET)
    @CrossOrigin(origins = "*")
    public ResponseEntity<GeneralResponse> login(Model model, String error, String logout) {
        System.out.println("LOGIN"+model.toString());

        if (error != null) {
            System.out.println("1.IF");
            model.addAttribute("error", "Your username and password is invalid.");
        }

        if (logout != null) {
            System.out.println("2.IF");
            model.addAttribute("message", "You have been logged out successfully.");
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new GeneralResponse(true, "FAILURE: Error"));
    }
Run Code Online (Sandbox Code Playgroud)

这是我的WebSecurityConfig: …

java spring spring-security ionic2 angular

20
推荐指数
1
解决办法
5233
查看次数

在Angular 5中发布形式urlencoded

我正在尝试使用Spring后端和Angular 5前端开发应用程序.对于登录我使用的是Spring Security,在前端我试图将登录数据发布为x-www-form-urlencoded.但是后端的用户名和密码都是null.Angular 5文档HttpClient仅提供了发布json数据的示例,并且Http已弃用.

任何有关如何解决这个问题的建议将不胜感激.

这是我的Angular代码:

constructor(public httpClient: HttpClient) {
    console.log('Hello RestService Provider');
}

login(username: string, password: string) {
    var body = 'username=' + username + '&password=' + password + "&remember-me=" +  false;
    var headers = new HttpHeaders();
    headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
    let options = {headers: headers, withCredentials: true};
    this.callPostWithOptions("login", body, options).then(data=>{
      this.getCurrentUser();
    });
}

callPostWithOptions(address, body, options) {
    return new Promise(resolve => {
      address = this._SERVER_HOST + ":" + this._SERVER_PORT + "/" + address; …
Run Code Online (Sandbox Code Playgroud)

rest spring urlencode angular

5
推荐指数
1
解决办法
2522
查看次数

使 Spring JUnit Test 在测试数据库上运行

我想将 JUnit 测试配置为在与应用程序运行的数据库不同的数据库上运行。我已经这样做了几个小时,但到目前为止还没有成功。尝试使用 application.properties 文件和 spring 配置文件配置它,但没有任何效果。那么如何才能实现这一点呢?任何帮助将不胜感激。

这是我的测试文件:

@RunWith(SpringRunner.class)
@SpringBootTest(properties = "spring.profiles.active=test")
public class TestContactController extends AbstractTest {

    @Autowired
    private MockMvc mvc;

    @Test
    @Rollback
    public void testAddContact() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/contact/add").contentType(MediaType.APPLICATION_JSON)
            .content("{\n" +
                    "\t\"authCode\": \"daca824a0bf04d038543373adfdb2c8f\",\n" +
                    "\t\"requestData\":{\n" +
                    "\t\t\"firstName\": \"Max\",\n" +
                    "\t\t\"lastName\": \"Mustermann\",\n" +
                    "\t\t\"category\": {\n" +
                    "\t\t\t\"id\": " + categoryId + "\n" +
                    "\t\t}, \"partOfOrgUnit\": {\n" +
                    "\t\t\t\"id\": " + companyId + "\n" +
                    "\t\t}\n" +
                    "\t}\n" +
                    "}"))
            .andExpect(status().isOk())
            .andExpect(content().string(equalTo("{\"success\":true,\"message\":\"SUCCESS: Contact added successfully\"}")));
    }
}
Run Code Online (Sandbox Code Playgroud)

我的持久性 xml,带有两个数据库:

<?xml …
Run Code Online (Sandbox Code Playgroud)

java junit hibernate spring-mvc spring-boot

4
推荐指数
1
解决办法
1万
查看次数