initLogin II
This commit is contained in:
1 parent
de012ac1b2
commit
22bfd452d5
23 files changed
+252
-12
No files matched your search
@@ -11,7 +11,7 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/user")
|
@RequestMapping("/api")
|
||||||
public class UserController {
|
public class UserController {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(UserController.class);
|
private static final Logger LOG = LoggerFactory.getLogger(UserController.class);
|
||||||
|
|
||||||
@@ -24,7 +24,7 @@ public class UserController {
|
|||||||
this.encoder = encoder;
|
this.encoder = encoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("")
|
@GetMapping("/user")
|
||||||
public List<ApplicationUser> getAll() {
|
public List<ApplicationUser> getAll() {
|
||||||
return applicationUserRepository.findAll();
|
return applicationUserRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,4 +65,12 @@ public class ApplicationUser {
|
|||||||
", password='" + password + '\'' +
|
", password='" + password + '\'' +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String json() {
|
||||||
|
return "{" +
|
||||||
|
"id=" + id +
|
||||||
|
", login='" + login + '\'' +
|
||||||
|
", password='" + password + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package ch.boxi.boxit.security;
|
package ch.boxi.boxit.security;
|
||||||
|
|
||||||
|
import ch.boxi.boxit.db.ApplicationUserRepository;
|
||||||
import ch.boxi.boxit.model.ApplicationUser;
|
import ch.boxi.boxit.model.ApplicationUser;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import io.jsonwebtoken.Jwts;
|
import io.jsonwebtoken.Jwts;
|
||||||
@@ -9,6 +10,7 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio
|
|||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.AuthenticationException;
|
import org.springframework.security.core.AuthenticationException;
|
||||||
import org.springframework.security.core.userdetails.User;
|
import org.springframework.security.core.userdetails.User;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
||||||
|
|
||||||
import javax.servlet.FilterChain;
|
import javax.servlet.FilterChain;
|
||||||
@@ -24,9 +26,12 @@ import static ch.boxi.boxit.security.SecurityConstants.*;
|
|||||||
public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
|
public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
|
||||||
|
|
||||||
private AuthenticationManager authenticationManager;
|
private AuthenticationManager authenticationManager;
|
||||||
|
private ApplicationUserRepository userRepository;
|
||||||
|
|
||||||
public JWTAuthenticationFilter(AuthenticationManager authenticationManager) {
|
public JWTAuthenticationFilter(AuthenticationManager authenticationManager, ApplicationUserRepository userRepository) {
|
||||||
this.authenticationManager = authenticationManager;
|
this.authenticationManager = authenticationManager;
|
||||||
|
this.userRepository = userRepository;
|
||||||
|
setFilterProcessesUrl("/api/login");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -53,11 +58,13 @@ public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilte
|
|||||||
FilterChain chain,
|
FilterChain chain,
|
||||||
Authentication auth) throws IOException, ServletException {
|
Authentication auth) throws IOException, ServletException {
|
||||||
|
|
||||||
|
String username = ((User) auth.getPrincipal()).getUsername();
|
||||||
String token = Jwts.builder()
|
String token = Jwts.builder()
|
||||||
.setSubject(((User) auth.getPrincipal()).getUsername())
|
.setSubject(username)
|
||||||
.setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME))
|
.setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME))
|
||||||
.signWith(SignatureAlgorithm.HS512, SECRET.getBytes())
|
.signWith(SignatureAlgorithm.HS512, SECRET.getBytes())
|
||||||
.compact();
|
.compact();
|
||||||
res.addHeader(HEADER_STRING, TOKEN_PREFIX + token);
|
res.addHeader(HEADER_STRING, TOKEN_PREFIX + token);
|
||||||
|
res.getOutputStream().print(userRepository.findByUsername(username).json());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package ch.boxi.boxit.security;
|
package ch.boxi.boxit.security;
|
||||||
|
|
||||||
|
import ch.boxi.boxit.db.ApplicationUserRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||||
@@ -20,11 +21,13 @@ import static ch.boxi.boxit.security.SecurityConstants.SIGN_UP_URL;
|
|||||||
public class WebSecurity extends WebSecurityConfigurerAdapter {
|
public class WebSecurity extends WebSecurityConfigurerAdapter {
|
||||||
private UserDetailsService userDetailsService;
|
private UserDetailsService userDetailsService;
|
||||||
private BCryptPasswordEncoder bCryptPasswordEncoder;
|
private BCryptPasswordEncoder bCryptPasswordEncoder;
|
||||||
|
private ApplicationUserRepository userRepository;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public WebSecurity(UserDetailsService userDetailsService, BCryptPasswordEncoder bCryptPasswordEncoder) {
|
public WebSecurity(UserDetailsService userDetailsService, BCryptPasswordEncoder bCryptPasswordEncoder, ApplicationUserRepository userRepository) {
|
||||||
this.userDetailsService = userDetailsService;
|
this.userDetailsService = userDetailsService;
|
||||||
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
|
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
|
||||||
|
this.userRepository = userRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -33,7 +36,7 @@ public class WebSecurity extends WebSecurityConfigurerAdapter {
|
|||||||
.antMatchers(HttpMethod.PUT, SIGN_UP_URL).permitAll()
|
.antMatchers(HttpMethod.PUT, SIGN_UP_URL).permitAll()
|
||||||
.anyRequest().authenticated()
|
.anyRequest().authenticated()
|
||||||
.and()
|
.and()
|
||||||
.addFilter(new JWTAuthenticationFilter(authenticationManager()))
|
.addFilter(new JWTAuthenticationFilter(authenticationManager(), userRepository))
|
||||||
.addFilter(new JWTAuthorizationFilter(authenticationManager(), userDetailsService))
|
.addFilter(new JWTAuthorizationFilter(authenticationManager(), userDetailsService))
|
||||||
// this disables session creation on Spring Security
|
// this disables session creation on Spring Security
|
||||||
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
|
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
"testTsconfig": "tsconfig.spec.json",
|
"testTsconfig": "tsconfig.spec.json",
|
||||||
"prefix": "app",
|
"prefix": "app",
|
||||||
"styles": [
|
"styles": [
|
||||||
|
"../node_modules/bootstrap/dist/css/bootstrap.css",
|
||||||
"styles.css"
|
"styles.css"
|
||||||
],
|
],
|
||||||
"scripts": [],
|
"scripts": [],
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ This project was generated with [Angular CLI](https://github.com/angular/angular
|
|||||||
## Development server
|
## Development server
|
||||||
|
|
||||||
Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files.
|
Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files.
|
||||||
|
Run `ng serve --proxy-config proxy-conf.json`
|
||||||
|
|
||||||
## Code scaffolding
|
## Code scaffolding
|
||||||
|
|
||||||
|
|||||||
Generated
+5
@@ -906,6 +906,11 @@
|
|||||||
"hoek": "2.16.3"
|
"hoek": "2.16.3"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"bootstrap": {
|
||||||
|
"version": "3.3.7",
|
||||||
|
"resolved": "https://registry.npmjs.org/bootstrap/-/bootstrap-3.3.7.tgz",
|
||||||
|
"integrity": "sha1-WjiTlFSfIzMIdaOxUGVldPip63E="
|
||||||
|
},
|
||||||
"brace-expansion": {
|
"brace-expansion": {
|
||||||
"version": "1.1.8",
|
"version": "1.1.8",
|
||||||
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.8.tgz",
|
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.8.tgz",
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"/api": {
|
||||||
|
"target": "http://localhost:8080",
|
||||||
|
"source": false
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import {PreloadAllModules, RouterModule, Routes} from "@angular/router";
|
||||||
|
import {HomeComponent} from "./core/home/home.component";
|
||||||
|
import {NgModule} from "@angular/core";
|
||||||
|
|
||||||
|
const appRoutes: Routes = [
|
||||||
|
{path: '', component: HomeComponent},
|
||||||
|
{path: 'auth', loadChildren: './auth/auth.module#AuthModule'}
|
||||||
|
]
|
||||||
|
|
||||||
|
@NgModule({
|
||||||
|
imports: [
|
||||||
|
RouterModule.forRoot(appRoutes, {preloadingStrategy: PreloadAllModules})
|
||||||
|
],
|
||||||
|
exports: [
|
||||||
|
RouterModule
|
||||||
|
]
|
||||||
|
})
|
||||||
|
export class AppRoutingModule {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
import { BrowserModule } from '@angular/platform-browser';
|
import { BrowserModule } from '@angular/platform-browser';
|
||||||
import { NgModule } from '@angular/core';
|
import { NgModule } from '@angular/core';
|
||||||
|
|
||||||
|
|
||||||
import { AppComponent } from './app.component';
|
import { AppComponent } from './app.component';
|
||||||
|
import {CoreModule} from "./core/core.module";
|
||||||
|
import {AppRoutingModule} from "./app-routing.module";
|
||||||
|
import {SharedModule} from "./shared/shared.module";
|
||||||
|
import {AuthService} from "./auth/auth.service";
|
||||||
|
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
@@ -10,9 +12,14 @@ import { AppComponent } from './app.component';
|
|||||||
AppComponent
|
AppComponent
|
||||||
],
|
],
|
||||||
imports: [
|
imports: [
|
||||||
BrowserModule
|
BrowserModule,
|
||||||
|
CoreModule,
|
||||||
|
SharedModule,
|
||||||
|
AppRoutingModule
|
||||||
|
],
|
||||||
|
providers: [
|
||||||
|
AuthService
|
||||||
],
|
],
|
||||||
providers: [],
|
|
||||||
bootstrap: [AppComponent]
|
bootstrap: [AppComponent]
|
||||||
})
|
})
|
||||||
export class AppModule { }
|
export class AppModule { }
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
import {RouterModule, Routes} from "@angular/router";
|
||||||
|
import {LoginComponent} from "./login/login.component";
|
||||||
|
import {NgModule} from "@angular/core";
|
||||||
|
|
||||||
|
const authRoutes: Routes = [
|
||||||
|
{path: 'login', component: LoginComponent}
|
||||||
|
];
|
||||||
|
|
||||||
|
@NgModule({
|
||||||
|
imports: [RouterModule.forChild(authRoutes)],
|
||||||
|
exports: [RouterModule]
|
||||||
|
})
|
||||||
|
export class AuthRoutingModule {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,10 +1,19 @@
|
|||||||
import { NgModule } from '@angular/core';
|
import { NgModule } from '@angular/core';
|
||||||
import { CommonModule } from '@angular/common';
|
import { CommonModule } from '@angular/common';
|
||||||
|
import { LoginComponent } from './login/login.component';
|
||||||
|
import {AuthRoutingModule} from "./auth-routing.module";
|
||||||
|
import {HttpClientModule} from "@angular/common/http";
|
||||||
|
import {ReactiveFormsModule} from "@angular/forms";
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
imports: [
|
imports: [
|
||||||
CommonModule
|
CommonModule,
|
||||||
|
HttpClientModule,
|
||||||
|
ReactiveFormsModule,
|
||||||
|
AuthRoutingModule
|
||||||
],
|
],
|
||||||
declarations: []
|
declarations: [
|
||||||
|
LoginComponent,
|
||||||
|
]
|
||||||
})
|
})
|
||||||
export class AuthModule { }
|
export class AuthModule { }
|
||||||
Whitespace-only changes.
@@ -0,0 +1,25 @@
|
|||||||
|
<form [formGroup]="loginForm" (ngSubmit)="onSubmit()">
|
||||||
|
<div class="col-sm-offset-4 col-sm-4 col-xs-offset-2 col-xs-8">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-xs-12">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="login">Login</label>
|
||||||
|
<input type="text" id="login" name="login" formControlName="login" class="form-control" placeholder="your Login"/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-xs-12">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="password">Password</label>
|
||||||
|
<input type="password" id="password" name="password" formControlName="password" class="form-control"/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-xs-12">
|
||||||
|
<button type="submit" class="btn btn-primary pull-right" [disabled]="!loginForm.valid">Login</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import { Component, OnInit } from '@angular/core';
|
||||||
|
import {FormControl, FormGroup, Validators} from "@angular/forms";
|
||||||
|
import {AuthService} from "../auth.service";
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'app-login',
|
||||||
|
templateUrl: './login.component.html',
|
||||||
|
styleUrls: ['./login.component.css']
|
||||||
|
})
|
||||||
|
export class LoginComponent implements OnInit {
|
||||||
|
|
||||||
|
loginForm: FormGroup;
|
||||||
|
|
||||||
|
constructor(public authService: AuthService) { }
|
||||||
|
|
||||||
|
ngOnInit() {
|
||||||
|
this.loginForm = new FormGroup({
|
||||||
|
'login': new FormControl("", Validators.required),
|
||||||
|
'password': new FormControl("", Validators.required)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public onSubmit() {
|
||||||
|
this.authService.login(this.loginForm.value.login, this.loginForm.value.password);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
import {HomeComponent} from "./home/home.component";
|
||||||
|
import {NgModule} from "@angular/core";
|
||||||
|
import {AuthService} from "../auth/auth.service";
|
||||||
|
import { HeaderComponent } from './header/header.component';
|
||||||
|
import {SharedModule} from "../shared/shared.module";
|
||||||
|
import {AppRoutingModule} from "../app-routing.module";
|
||||||
|
|
||||||
|
@NgModule({
|
||||||
|
declarations: [
|
||||||
|
HomeComponent,
|
||||||
|
HeaderComponent
|
||||||
|
],
|
||||||
|
imports: [
|
||||||
|
SharedModule,
|
||||||
|
AppRoutingModule
|
||||||
|
],
|
||||||
|
exports: [
|
||||||
|
HeaderComponent,
|
||||||
|
AppRoutingModule
|
||||||
|
]
|
||||||
|
})
|
||||||
|
export class CoreModule{
|
||||||
|
|
||||||
|
}
|
||||||
Whitespace-only changes.
@@ -0,0 +1,28 @@
|
|||||||
|
<div class="navbar navbar-default">
|
||||||
|
<div class="container-fluid">
|
||||||
|
<div class="navbar-header">
|
||||||
|
<a routerLink="/" class="navbar-brand">Boxit</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="collapse navbar-collapse">
|
||||||
|
<ul class="nav navbar-nav">
|
||||||
|
<li routerLinkActive="active"><a routerLink="/">Link1</a></li>
|
||||||
|
<li routerLinkActive="active"><a routerLink="/">Link2</a></li>
|
||||||
|
</ul>
|
||||||
|
<ul class="nav navbar-nav navbar-right">
|
||||||
|
<ng-template [ngIf]="!isAuthenticated()">
|
||||||
|
<li><a routerLink="/signup">Register</a></li>
|
||||||
|
<li><a routerLink="/auth/login">Login</a></li>
|
||||||
|
</ng-template>
|
||||||
|
<!--<li *ngIf="isAuthenticated()"><a style="cursor: pointer" (click)="onLogout()">Logout</a></li>-->
|
||||||
|
<!--<li class="dropdown" appDropdown *ngIf="isAuthenticated()">-->
|
||||||
|
<!--<a style="cursor: pointer" class="dropdown-toggle" role="button">Manage <span class="caret"></span></a>-->
|
||||||
|
<!--<ul class="dropdown-menu">-->
|
||||||
|
<!--<li><a style="cursor: pointer" (click)="onSaveData()">Save Data</a></li>-->
|
||||||
|
<!--<li><a style="cursor: pointer" (click)="onLoadData()">Fetch Data</a></li>-->
|
||||||
|
<!--</ul>-->
|
||||||
|
<!--</li>-->
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import { Component, OnInit } from '@angular/core';
|
||||||
|
import {AuthService} from "../../auth/auth.service";
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'app-header',
|
||||||
|
templateUrl: './header.component.html',
|
||||||
|
styleUrls: ['./header.component.css']
|
||||||
|
})
|
||||||
|
export class HeaderComponent implements OnInit {
|
||||||
|
|
||||||
|
constructor(private authService:AuthService) { }
|
||||||
|
|
||||||
|
ngOnInit() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public isAuthenticated():boolean {
|
||||||
|
return this.authService.isAuthenticated();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Whitespace-only changes.
@@ -0,0 +1,4 @@
|
|||||||
|
<div class="col-xs-12">
|
||||||
|
home works!
|
||||||
|
{{authService.isAuthenticated()}}
|
||||||
|
</div>
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import { Component, OnInit } from '@angular/core';
|
||||||
|
import {AuthService} from "../../auth/auth.service";
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'app-home',
|
||||||
|
templateUrl: './home.component.html',
|
||||||
|
styleUrls: ['./home.component.css']
|
||||||
|
})
|
||||||
|
export class HomeComponent implements OnInit {
|
||||||
|
|
||||||
|
constructor(public authService: AuthService) { }
|
||||||
|
|
||||||
|
ngOnInit() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import { NgModule } from '@angular/core';
|
||||||
|
import { CommonModule } from '@angular/common';
|
||||||
|
import {AuthModule} from "../auth/auth.module";
|
||||||
|
|
||||||
|
@NgModule({
|
||||||
|
imports: [
|
||||||
|
],
|
||||||
|
exports: [
|
||||||
|
AuthModule,
|
||||||
|
CommonModule
|
||||||
|
],
|
||||||
|
declarations: []
|
||||||
|
})
|
||||||
|
export class SharedModule { }
|
||||||