Guest User

Untitled

a guest
Nov 20th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. @CrossOrigin(origins = "http://localhost:4200")
  2. @GetMapping("/checkUsers")
  3. public String checkLogin() throws JsonProcessingException {
  4.  
  5. ObjectMapper mapper = new ObjectMapper();
  6. List<Users> useObj = (List<Users>) userRepo.findAll();
  7. return(mapper.writeValueAsString(useObj));
  8. }
  9.  
  10. @CrossOrigin(origins = "http://localhost:4200")
  11. @RequestMapping(value = "/checkByCredential", method = RequestMethod.POST)
  12. public String checkLoginByName(@RequestBody Users user) throws Exception{
  13.  
  14. ObjectMapper mapper = new ObjectMapper();
  15. Users useObj1 =
  16. userRepo.findByUsernameAndPassword(user.username,user.password);
  17. return(mapper.writeValueAsString(useObj1));
  18. }
  19.  
  20. import { BrowserModule } from '@angular/platform-browser';
  21. import { NgModule } from '@angular/core';
  22.  
  23. import { HttpModule } from '@angular/http';
  24. import { FormsModule } from '@angular/forms';
  25.  
  26. import { AppComponent } from './app.component';
  27. import { LoginpostComponent } from './loginpost.component';
  28. import { BasicService } from './basic.service';
  29.  
  30.  
  31. @NgModule({
  32.  
  33. declarations : [AppComponent, LoginpostComponent ],
  34. imports : [BrowserModule,HttpModule,FormsModule ],
  35. providers : [BasicService],
  36. bootstrap : [LoginpostComponent]
  37.  
  38. })
  39.  
  40. export class AppModule { }
  41.  
  42. import { Component } from '@angular/core';
  43. import { BasicService } from './basic.service';
  44.  
  45. @Component({
  46. selector: 'app-root',
  47. template: `<h1>{{ title }}</h1>
  48. <div style="height: 200px; overflow: auto;">
  49. <table *ngFor= " let item of data">
  50. <tr><td>Username</td><td>{{ item.username }}</td></tr>
  51. <tr><td>Password</td><td>{{ item.password }}</td></tr>
  52. </table>
  53. </div>`
  54.  
  55. })
  56. export class AppComponent {
  57. title :string;
  58. data:any;
  59.  
  60. constructor(private MyService: BasicService){
  61. this.title="Angular Service";
  62.  
  63. this.MyService.GetUsers()
  64. .subscribe(users => {this.data = users });
  65. }
  66. }
  67.  
  68. import { Injectable } from '@angular/core';
  69. import { Http } from '@angular/http';
  70. import 'rxjs/add/operator/map';
  71.  
  72. @Injectable()
  73. export class BasicService {
  74.  
  75. constructor(private http:Http) { }
  76.  
  77. GetUsers(){
  78. return this.http.get('http://localhost:8080/checkUsers')
  79. .map(result => result.json());
  80. }
  81.  
  82.  
  83. }
  84.  
  85. import { Component } from '@angular/core';
  86. import { Http, Headers } from '@angular/http';
  87.  
  88. @Component({
  89. selector: 'app',
  90. template:`<app-root></app-root>
  91. <h2>Login</h2>
  92. <form role="form">
  93. <div ng-control-group="credentials">
  94. <label for="username">Username</label>
  95. <input
  96. type="text"
  97. #username
  98. id="username"
  99. ng-control="username"
  100. required>
  101.  
  102. <label for="password">Password</label>
  103. <input
  104. type="password"
  105. #password
  106. id="password"
  107. ng-control="password"
  108. required>
  109. </div>
  110. <button (click)="authenticate(username, password)">Login!</button>
  111.  
  112. </form>`
  113. })
  114.  
  115.  
  116. export class LoginpostComponent {
  117. title: string;
  118. data: string;
  119. username: string;
  120. password: string;
  121.  
  122.  
  123. constructor(public http: Http) { }
  124.  
  125.  
  126. authenticate(username, password) {
  127.  
  128. let creds = JSON.stringify({ username: username.value, password: password.value });
  129.  
  130. let headers = new Headers();
  131. headers.append('Content-Type', 'application/json');
  132.  
  133. this.http.post('http://localhost:8080/checkByCredential', creds, {headers: headers})
  134. .subscribe(data => {
  135. username.value = '';
  136. password.value = '';
  137. },
  138. () => console.log()
  139. );
  140. }
  141.  
  142. }
  143. ;
Add Comment
Please, Sign In to add comment