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;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
@RequestMapping("/api")
|
||||
public class UserController {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(UserController.class);
|
||||
|
||||
@@ -24,7 +24,7 @@ public class UserController {
|
||||
this.encoder = encoder;
|
||||
}
|
||||
|
||||
@GetMapping("")
|
||||
@GetMapping("/user")
|
||||
public List<ApplicationUser> getAll() {
|
||||
return applicationUserRepository.findAll();
|
||||
}
|
||||
|
||||
@@ -65,4 +65,12 @@ public class ApplicationUser {
|
||||
", password='" + password + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
public String json() {
|
||||
return "{" +
|
||||
"id=" + id +
|
||||
", login='" + login + '\'' +
|
||||
", password='" + password + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package ch.boxi.boxit.security;
|
||||
|
||||
import ch.boxi.boxit.db.ApplicationUserRepository;
|
||||
import ch.boxi.boxit.model.ApplicationUser;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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.AuthenticationException;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
@@ -24,9 +26,12 @@ import static ch.boxi.boxit.security.SecurityConstants.*;
|
||||
public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
|
||||
|
||||
private AuthenticationManager authenticationManager;
|
||||
private ApplicationUserRepository userRepository;
|
||||
|
||||
public JWTAuthenticationFilter(AuthenticationManager authenticationManager) {
|
||||
public JWTAuthenticationFilter(AuthenticationManager authenticationManager, ApplicationUserRepository userRepository) {
|
||||
this.authenticationManager = authenticationManager;
|
||||
this.userRepository = userRepository;
|
||||
setFilterProcessesUrl("/api/login");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -53,11 +58,13 @@ public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilte
|
||||
FilterChain chain,
|
||||
Authentication auth) throws IOException, ServletException {
|
||||
|
||||
String username = ((User) auth.getPrincipal()).getUsername();
|
||||
String token = Jwts.builder()
|
||||
.setSubject(((User) auth.getPrincipal()).getUsername())
|
||||
.setSubject(username)
|
||||
.setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME))
|
||||
.signWith(SignatureAlgorithm.HS512, SECRET.getBytes())
|
||||
.compact();
|
||||
res.addHeader(HEADER_STRING, TOKEN_PREFIX + token);
|
||||
res.getOutputStream().print(userRepository.findByUsername(username).json());
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package ch.boxi.boxit.security;
|
||||
|
||||
import ch.boxi.boxit.db.ApplicationUserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpMethod;
|
||||
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 {
|
||||
private UserDetailsService userDetailsService;
|
||||
private BCryptPasswordEncoder bCryptPasswordEncoder;
|
||||
private ApplicationUserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
public WebSecurity(UserDetailsService userDetailsService, BCryptPasswordEncoder bCryptPasswordEncoder) {
|
||||
public WebSecurity(UserDetailsService userDetailsService, BCryptPasswordEncoder bCryptPasswordEncoder, ApplicationUserRepository userRepository) {
|
||||
this.userDetailsService = userDetailsService;
|
||||
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -33,7 +36,7 @@ public class WebSecurity extends WebSecurityConfigurerAdapter {
|
||||
.antMatchers(HttpMethod.PUT, SIGN_UP_URL).permitAll()
|
||||
.anyRequest().authenticated()
|
||||
.and()
|
||||
.addFilter(new JWTAuthenticationFilter(authenticationManager()))
|
||||
.addFilter(new JWTAuthenticationFilter(authenticationManager(), userRepository))
|
||||
.addFilter(new JWTAuthorizationFilter(authenticationManager(), userDetailsService))
|
||||
// this disables session creation on Spring Security
|
||||
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
"testTsconfig": "tsconfig.spec.json",
|
||||
"prefix": "app",
|
||||
"styles": [
|
||||
"../node_modules/bootstrap/dist/css/bootstrap.css",
|
||||
"styles.css"
|
||||
],
|
||||
"scripts": [],
|
||||
|
||||
@@ -5,6 +5,7 @@ This project was generated with [Angular CLI](https://github.com/angular/angular
|
||||
## 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 --proxy-config proxy-conf.json`
|
||||
|
||||
## Code scaffolding
|
||||
|
||||
|
||||
Generated
+5
@@ -906,6 +906,11 @@
|
||||
"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": {
|
||||
"version": "1.1.8",
|
||||
"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 { NgModule } from '@angular/core';
|
||||
|
||||
|
||||
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({
|
||||
@@ -10,9 +12,14 @@ import { AppComponent } from './app.component';
|
||||
AppComponent
|
||||
],
|
||||
imports: [
|
||||
BrowserModule
|
||||
BrowserModule,
|
||||
CoreModule,
|
||||
SharedModule,
|
||||
AppRoutingModule
|
||||
],
|
||||
providers: [
|
||||
AuthService
|
||||
],
|
||||
providers: [],
|
||||
bootstrap: [AppComponent]
|
||||
})
|
||||
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 { 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({
|
||||
imports: [
|
||||
CommonModule
|
||||
CommonModule,
|
||||
HttpClientModule,
|
||||
ReactiveFormsModule,
|
||||
AuthRoutingModule
|
||||
],
|
||||
declarations: []
|
||||
declarations: [
|
||||
LoginComponent,
|
||||
]
|
||||
})
|
||||
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 { }
|
||||