diff --git a/spring-security-anguar/client/anguarjs/app.js b/spring-security-anguar/client/anguarjs/app.js new file mode 100644 index 0000000000..4b7268da3f --- /dev/null +++ b/spring-security-anguar/client/anguarjs/app.js @@ -0,0 +1,40 @@ +(function () { + 'use strict'; + + angular + .module('app', ['ngRoute']) + .config(config) + .run(run); + + config.$inject = ['$routeProvider', '$locationProvider']; + function config($routeProvider, $locationProvider) { + $routeProvider + .when('/', { + controller: 'HomeController', + templateUrl: 'home/home.view.html', + controllerAs: 'vm' + }) + .when('/login', { + controller: 'LoginController', + templateUrl: 'login/login.view.html', + controllerAs: 'vm' + }) + .otherwise({ redirectTo: '/login' }); + } + + run.$inject = ['$rootScope', '$location', '$http', '$window']; + function run($rootScope, $location, $http, $window) { + var userData = $window.sessionStorage.getItem('userData'); + if (userData) { + $http.defaults.headers.common['Authorization'] = 'Basic ' + JSON.parse(userData).authData; + } + + $rootScope.$on('$locationChangeStart', function (event, next, current) { + var restrictedPage = $.inArray($location.path(), ['/login']) === -1; + var loggedIn = $window.sessionStorage.getItem('userData');; + if (restrictedPage && !loggedIn) { + $location.path('/login'); + } + }); + } +})(); \ No newline at end of file diff --git a/spring-security-anguar/client/anguarjs/home/home.controller.js b/spring-security-anguar/client/anguarjs/home/home.controller.js new file mode 100644 index 0000000000..6449029ec2 --- /dev/null +++ b/spring-security-anguar/client/anguarjs/home/home.controller.js @@ -0,0 +1,34 @@ +(function () { + 'use strict'; + + angular + .module('app') + .controller('HomeController', HomeController); + + HomeController.$inject = ['$window', '$http', '$scope']; + function HomeController($window, $http, $scope) { + var vm = this; + + vm.user = null; + + initController(); + + function initController() { + + $http({ + url: 'http://localhost:8082/user', + method: "GET" + }).then(function (response) { + vm.user = response.data.name; + },function(error){ + console.log(error); + }); + }; + + $scope.logout = function(){ + $window.sessionStorage.setItem('userData', ''); + $http.defaults.headers.common['Authorization'] = 'Basic'; + } + } + +})(); \ No newline at end of file diff --git a/spring-security-anguar/client/anguarjs/home/home.view.html b/spring-security-anguar/client/anguarjs/home/home.view.html new file mode 100644 index 0000000000..bb5c2d3dbf --- /dev/null +++ b/spring-security-anguar/client/anguarjs/home/home.view.html @@ -0,0 +1,3 @@ +

Hi {{vm.user}}!

+

You're logged in!!

+

Logout

\ No newline at end of file diff --git a/spring-security-anguar/client/anguarjs/index.html b/spring-security-anguar/client/anguarjs/index.html new file mode 100644 index 0000000000..a5584d53be --- /dev/null +++ b/spring-security-anguar/client/anguarjs/index.html @@ -0,0 +1,17 @@ + + + + + + +
+ + + + + + + + + + \ No newline at end of file diff --git a/spring-security-anguar/client/anguarjs/login/login.controller.js b/spring-security-anguar/client/anguarjs/login/login.controller.js new file mode 100644 index 0000000000..2da509ec83 --- /dev/null +++ b/spring-security-anguar/client/anguarjs/login/login.controller.js @@ -0,0 +1,38 @@ +(function () { + 'use strict'; + + angular + .module('app') + .controller('LoginController', LoginController); + + LoginController.$inject = ['$location', '$window', '$http']; + function LoginController($location, $window, $http) { + var vm = this; + vm.login = login; + + (function initController() { + $window.localStorage.setItem('token', ''); + })(); + + function login() { + $http({ + url: 'http://localhost:8082/login', + method: "POST", + data: { 'userName': vm.username, 'password': vm.password } + }).then(function (response) { + if (response.data) { + var token = $window.btoa(vm.username + ':' + vm.password); + var userData = { + userName: vm.username, + authData: token + } + $window.sessionStorage.setItem('userData', JSON.stringify(userData)); + $http.defaults.headers.common['Authorization'] = 'Basic ' + token; + $location.path('/'); + } else { + alert("Authentication failed.") + } + }); + }; + } +})(); diff --git a/spring-security-anguar/client/anguarjs/login/login.view.html b/spring-security-anguar/client/anguarjs/login/login.view.html new file mode 100644 index 0000000000..0d42f29b54 --- /dev/null +++ b/spring-security-anguar/client/anguarjs/login/login.view.html @@ -0,0 +1,18 @@ +
+

Login

+
+
+ + + Username is required +
+
+ + + Password is required +
+
+ +
+
+
\ No newline at end of file diff --git a/spring-security-anguar/client/angular2/app.css b/spring-security-anguar/client/angular2/app.css new file mode 100644 index 0000000000..cdd2d591d8 --- /dev/null +++ b/spring-security-anguar/client/angular2/app.css @@ -0,0 +1,7 @@ +a { + cursor: pointer; +} + +.help-block { + font-size: 12px; +} \ No newline at end of file diff --git a/spring-security-anguar/client/angular2/app/app.component.html b/spring-security-anguar/client/angular2/app/app.component.html new file mode 100644 index 0000000000..7f77adea7b --- /dev/null +++ b/spring-security-anguar/client/angular2/app/app.component.html @@ -0,0 +1,7 @@ +
+
+
+ +
+
+
diff --git a/spring-security-anguar/client/angular2/app/app.component.ts b/spring-security-anguar/client/angular2/app/app.component.ts new file mode 100644 index 0000000000..0aae2b6992 --- /dev/null +++ b/spring-security-anguar/client/angular2/app/app.component.ts @@ -0,0 +1,8 @@ +import { Component } from '@angular/core'; + +@Component({ + selector: 'app', + templateUrl: './app/app.component.html' +}) + +export class AppComponent { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular2/app/app.module.ts b/spring-security-anguar/client/angular2/app/app.module.ts new file mode 100644 index 0000000000..4d484b49f8 --- /dev/null +++ b/spring-security-anguar/client/angular2/app/app.module.ts @@ -0,0 +1,27 @@ +import { NgModule } from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; +import { FormsModule } from '@angular/forms'; +import { HttpModule } from '@angular/http'; + +import { AppComponent } from './app.component'; +import { routing } from './app.routing'; + +import { HomeComponent } from './home/home.component'; +import { LoginComponent } from './login/login.component'; + +@NgModule({ + imports: [ + BrowserModule, + FormsModule, + HttpModule, + routing + ], + declarations: [ + AppComponent, + HomeComponent, + LoginComponent + ], + bootstrap: [AppComponent] +}) + +export class AppModule { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular2/app/app.routing.ts b/spring-security-anguar/client/angular2/app/app.routing.ts new file mode 100644 index 0000000000..c794fc5c50 --- /dev/null +++ b/spring-security-anguar/client/angular2/app/app.routing.ts @@ -0,0 +1,12 @@ +import { Routes, RouterModule } from '@angular/router'; + +import { HomeComponent } from './home/home.component'; +import { LoginComponent } from './login/login.component'; + +const appRoutes: Routes = [ + { path: '', component: HomeComponent}, + { path: 'login', component: LoginComponent }, + { path: '**', redirectTo: '' } +]; + +export const routing = RouterModule.forRoot(appRoutes); \ No newline at end of file diff --git a/spring-security-anguar/client/angular2/app/home/home.component.html b/spring-security-anguar/client/angular2/app/home/home.component.html new file mode 100644 index 0000000000..5f3b24be3a --- /dev/null +++ b/spring-security-anguar/client/angular2/app/home/home.component.html @@ -0,0 +1,4 @@ +
+

Hi {{userName}}!

+

Logout

+
\ No newline at end of file diff --git a/spring-security-anguar/client/angular2/app/home/home.component.ts b/spring-security-anguar/client/angular2/app/home/home.component.ts new file mode 100644 index 0000000000..1b168bfb30 --- /dev/null +++ b/spring-security-anguar/client/angular2/app/home/home.component.ts @@ -0,0 +1,36 @@ +import { Component, OnInit } from '@angular/core'; +import { Http, RequestOptions, Headers } from '@angular/http'; +import 'rxjs/add/operator/map' + +@Component({ + selector:'home', + templateUrl: './app/home/home.component.html' +}) + +export class HomeComponent implements OnInit { + + userName: string; + + constructor(private http: Http) { } + + ngOnInit() { + let url = 'http://localhost:8082/user'; + let headers = new Headers({ + 'Authorization': 'Basic ' + sessionStorage.getItem('token') + }); + let options = new RequestOptions({ headers: headers }); + this.http.post(url,{}, options). + map(res => res.json()). + subscribe( + principal => this.userName = principal.name, + error => { + if(error.status == 401) + alert('Unauthorized'); + } + ); + } + + logout() { + sessionStorage.setItem('token', ''); + } +} \ No newline at end of file diff --git a/spring-security-anguar/client/angular2/app/login/login.component.html b/spring-security-anguar/client/angular2/app/login/login.component.html new file mode 100644 index 0000000000..d87b91a7bb --- /dev/null +++ b/spring-security-anguar/client/angular2/app/login/login.component.html @@ -0,0 +1,18 @@ +
+

Login

+
+
+ + +
Username is required
+
+
+ + +
Password is required
+
+
+ +
+
+
diff --git a/spring-security-anguar/client/angular2/app/login/login.component.ts b/spring-security-anguar/client/angular2/app/login/login.component.ts new file mode 100644 index 0000000000..81d6c1d7ae --- /dev/null +++ b/spring-security-anguar/client/angular2/app/login/login.component.ts @@ -0,0 +1,36 @@ +import { Component, OnInit } from '@angular/core'; +import { Router, ActivatedRoute } from '@angular/router'; +import { Http } from '@angular/http'; + +@Component({ + selector: 'login', + templateUrl: './app/login/login.component.html' +}) + +export class LoginComponent implements OnInit { + model: any = {}; + + constructor( + private route: ActivatedRoute, + private router: Router, + private http: Http) { } + + ngOnInit() { + sessionStorage.setItem('token', ''); + } + + login() { + let url = 'http://localhost:8082/login'; + let result = this.http.post(url, { + userName: this.model.username, + password: this.model.password + }).map(res => res.json()).subscribe(isValid => { + if (isValid) { + sessionStorage.setItem('token', btoa(this.model.username + ':' + this.model.password)); + this.router.navigate(['']); + } else { + alert("Authentication failed."); + } + }); + } +} diff --git a/spring-security-anguar/client/angular2/app/main.ts b/spring-security-anguar/client/angular2/app/main.ts new file mode 100644 index 0000000000..3a6c754786 --- /dev/null +++ b/spring-security-anguar/client/angular2/app/main.ts @@ -0,0 +1,5 @@ +import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; + +import { AppModule } from './app.module'; + +platformBrowserDynamic().bootstrapModule(AppModule); \ No newline at end of file diff --git a/spring-security-anguar/client/angular2/index.html b/spring-security-anguar/client/angular2/index.html new file mode 100644 index 0000000000..c9730a8568 --- /dev/null +++ b/spring-security-anguar/client/angular2/index.html @@ -0,0 +1,17 @@ + + + + + + + + + + + + + Loading... + + diff --git a/spring-security-anguar/client/angular2/package.json b/spring-security-anguar/client/angular2/package.json new file mode 100644 index 0000000000..9b36652fac --- /dev/null +++ b/spring-security-anguar/client/angular2/package.json @@ -0,0 +1,37 @@ +{ + "name": "angular2-quickstart", + "version": "1.0.0", + "description": "Sample of how easy and fast to start hacking with Angular2 and ng2-bootstrap", + "scripts": { + "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ", + "lite": "lite-server", + "tsc": "tsc", + "tsc:w": "tsc -w" + }, + "license": "ISC", + "dependencies": { + "@angular/common": "2.4.4", + "@angular/compiler": "2.4.4", + "@angular/core": "2.4.4", + "@angular/forms": "2.4.4", + "@angular/http": "2.4.4", + "@angular/platform-browser": "2.4.4", + "@angular/platform-browser-dynamic": "2.4.4", + "@angular/router": "3.4.4", + "@angular/upgrade": "2.4.4", + "angular2-in-memory-web-api": "0.0.21", + "bootstrap": "^3.3.7", + "core-js": "^2.4.1", + "ng2-bootstrap": "1.3.0", + "reflect-metadata": "^0.1.8", + "rxjs": "5.0.3", + "systemjs": "0.20.0", + "zone.js": "0.7.6" + }, + "devDependencies": { + "concurrently": "3.1.0", + "lite-server": "^2.2.0", + "typescript": "2.1.5" + }, + "repository": {} +} diff --git a/spring-security-anguar/client/angular2/systemjs.config.js b/spring-security-anguar/client/angular2/systemjs.config.js new file mode 100644 index 0000000000..ea4c543fdd --- /dev/null +++ b/spring-security-anguar/client/angular2/systemjs.config.js @@ -0,0 +1,41 @@ +/** + * System configuration for Angular 2 samples + * Adjust as necessary for your application needs. + */ +(function (global) { + System.config({ + paths: { + // paths serve as alias + 'npm:': 'node_modules/' + }, + // map tells the System loader where to look for things + map: { + // our app is within the app folder + app: 'app', + + // angular bundles + '@angular/core': 'npm:@angular/core/bundles/core.umd.js', + '@angular/common': 'npm:@angular/common/bundles/common.umd.js', + '@angular/http': 'npm:@angular/http/bundles/http.umd.js', + '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js', + '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js', + '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js', + '@angular/router': 'npm:@angular/router/bundles/router.umd.js', + '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js', + + // other libraries + 'rxjs': 'npm:rxjs', + 'tslib': 'npm:tslib/tslib.js' + }, + // packages tells the System loader how to load when no filename and/or no extension + packages: { + app: { + main: './main.js', + defaultExtension: 'js' + }, + rxjs: { + defaultExtension: 'js' + } + } + }); +})(this); diff --git a/spring-security-anguar/client/angular2/tsconfig.json b/spring-security-anguar/client/angular2/tsconfig.json new file mode 100644 index 0000000000..55f9ad70c3 --- /dev/null +++ b/spring-security-anguar/client/angular2/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "lib": [ "es2015", "dom" ], + "module": "commonjs", + "moduleResolution": "node", + "noImplicitAny": true, + "sourceMap": true, + "suppressImplicitAnyIndexErrors": true, + "target": "es5" + }, + "exclude": [ + "node_modules/*" + ] +} \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/.angular-cli.json b/spring-security-anguar/client/angular4/.angular-cli.json new file mode 100644 index 0000000000..967934b2f7 --- /dev/null +++ b/spring-security-anguar/client/angular4/.angular-cli.json @@ -0,0 +1,65 @@ +{ + "$schema": "./node_modules/@angular/cli/lib/config/schema.json", + "project": { + "name": "angular-crud" + }, + "apps": [ + { + "root": "src", + "outDir": "dist", + "assets": [ + "assets", + "favicon.ico" + ], + "index": "index.html", + "main": "main.ts", + "polyfills": "polyfills.ts", + "test": "test.ts", + "tsconfig": "tsconfig.app.json", + "testTsconfig": "tsconfig.spec.json", + "prefix": "app", + "styles": [ + "../node_modules/bootstrap/dist/css/bootstrap.min.css", + "styles.css", + "../node_modules/font-awesome/css/font-awesome.min.css" + ], + "scripts": [ + "../node_modules/jquery/dist/jquery.js", + "../node_modules/bootstrap/dist/js/bootstrap.js" + ], + "environmentSource": "environments/environment.ts", + "environments": { + "dev": "environments/environment.ts", + "prod": "environments/environment.prod.ts" + } + } + ], + "e2e": { + "protractor": { + "config": "./protractor.conf.js" + } + }, + "lint": [ + { + "project": "src/tsconfig.app.json", + "exclude": "**/node_modules/**" + }, + { + "project": "src/tsconfig.spec.json", + "exclude": "**/node_modules/**" + }, + { + "project": "e2e/tsconfig.e2e.json", + "exclude": "**/node_modules/**" + } + ], + "test": { + "karma": { + "config": "./karma.conf.js" + } + }, + "defaults": { + "styleExt": "css", + "component": {} + } +} \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/package.json b/spring-security-anguar/client/angular4/package.json new file mode 100644 index 0000000000..8c6ddcaf1f --- /dev/null +++ b/spring-security-anguar/client/angular4/package.json @@ -0,0 +1,54 @@ +{ + "name": "angular-crud", + "version": "0.0.0", + "license": "MIT", + "scripts": { + "ng": "ng", + "start": "ng serve", + "build": "ng build", + "test": "ng test", + "lint": "ng lint", + "e2e": "ng e2e" + }, + "private": true, + "dependencies": { + "@angular/animations": "^4.0.0", + "@angular/common": "^4.0.0", + "@angular/compiler": "^4.0.0", + "@angular/core": "^4.0.0", + "@angular/forms": "^4.0.0", + "@angular/http": "^4.0.0", + "@angular/platform-browser": "^4.0.0", + "@angular/platform-browser-dynamic": "^4.0.0", + "@angular/router": "^4.0.0", + "bootstrap": "^3.3.7", + "core-js": "^2.4.1", + "font-awesome": "^4.7.0", + "jquery": "^3.2.1", + "lodash": "^4.17.4", + "rxjs": "^5.4.1", + "zone.js": "^0.8.14" + }, + "devDependencies": { + "@angular/cli": "1.2.3", + "@angular/compiler-cli": "^4.0.0", + "@angular/language-service": "^4.0.0", + "@types/jasmine": "~2.5.53", + "@types/jasminewd2": "~2.0.2", + "@types/lodash": "^4.14.66", + "@types/node": "~6.0.60", + "codelyzer": "~3.0.1", + "jasmine-core": "~2.6.2", + "jasmine-spec-reporter": "~4.1.0", + "karma": "~1.7.0", + "karma-chrome-launcher": "~2.1.1", + "karma-cli": "~1.0.1", + "karma-coverage-istanbul-reporter": "^1.2.1", + "karma-jasmine": "~1.1.0", + "karma-jasmine-html-reporter": "^0.2.2", + "protractor": "~5.1.2", + "ts-node": "~3.0.4", + "tslint": "~5.3.2", + "typescript": "~2.3.3" + } +} diff --git a/spring-security-anguar/client/angular4/src/app/app.component.html b/spring-security-anguar/client/angular4/src/app/app.component.html new file mode 100644 index 0000000000..7f77adea7b --- /dev/null +++ b/spring-security-anguar/client/angular4/src/app/app.component.html @@ -0,0 +1,7 @@ +
+
+
+ +
+
+
diff --git a/spring-security-anguar/client/angular4/src/app/app.component.ts b/spring-security-anguar/client/angular4/src/app/app.component.ts new file mode 100644 index 0000000000..36f986c63f --- /dev/null +++ b/spring-security-anguar/client/angular4/src/app/app.component.ts @@ -0,0 +1,8 @@ +import { Component } from '@angular/core'; + +@Component({ + selector: 'app-root', + templateUrl: './app.component.html' +}) + +export class AppComponent { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/app/app.module.ts b/spring-security-anguar/client/angular4/src/app/app.module.ts new file mode 100644 index 0000000000..9e840e6f13 --- /dev/null +++ b/spring-security-anguar/client/angular4/src/app/app.module.ts @@ -0,0 +1,29 @@ +import { NgModule } from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; +import { FormsModule } from '@angular/forms'; +import { HttpModule } from '@angular/http'; + +// used to create fake backend + +import { AppComponent } from './app.component'; +import { routing } from './app.routing'; + +import { HomeComponent } from './home/home.component'; +import { LoginComponent } from './login/login.component'; + +@NgModule({ + imports: [ + BrowserModule, + FormsModule, + HttpModule, + routing + ], + declarations: [ + AppComponent, + HomeComponent, + LoginComponent + ], + bootstrap: [AppComponent] +}) + +export class AppModule { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/app/app.routing.ts b/spring-security-anguar/client/angular4/src/app/app.routing.ts new file mode 100644 index 0000000000..c794fc5c50 --- /dev/null +++ b/spring-security-anguar/client/angular4/src/app/app.routing.ts @@ -0,0 +1,12 @@ +import { Routes, RouterModule } from '@angular/router'; + +import { HomeComponent } from './home/home.component'; +import { LoginComponent } from './login/login.component'; + +const appRoutes: Routes = [ + { path: '', component: HomeComponent}, + { path: 'login', component: LoginComponent }, + { path: '**', redirectTo: '' } +]; + +export const routing = RouterModule.forRoot(appRoutes); \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/app/home/home.component.html b/spring-security-anguar/client/angular4/src/app/home/home.component.html new file mode 100644 index 0000000000..7ccd2c2a3a --- /dev/null +++ b/spring-security-anguar/client/angular4/src/app/home/home.component.html @@ -0,0 +1,4 @@ +
+

Hi {{userName}}!

+

Logout

+
\ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/app/home/home.component.ts b/spring-security-anguar/client/angular4/src/app/home/home.component.ts new file mode 100644 index 0000000000..0f66f42ce2 --- /dev/null +++ b/spring-security-anguar/client/angular4/src/app/home/home.component.ts @@ -0,0 +1,37 @@ +import { Component, OnInit } from '@angular/core'; +import { Http, RequestOptions, Headers } from '@angular/http'; +import 'rxjs/add/operator/map' + +@Component({ + selector:'home', + templateUrl: './home.component.html' +}) + +export class HomeComponent implements OnInit { + + userName: string; + + constructor(private http: Http) { } + + ngOnInit() { + let url = 'http://localhost:8082/user'; + let headers = new Headers({ + 'Authorization': 'Basic ' + sessionStorage.getItem('token') + }); + let options = new RequestOptions({ headers: headers }); + this.http.post(url,{}, options). + map( + res => res.json(), + error => { + if(error.status == 401) + alert('Unauthorized'); + } + ).subscribe(principal => { + this.userName = principal.name; + }); + } + + logout() { + sessionStorage.setItem('token', ''); + } +} \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/app/login/login.component.html b/spring-security-anguar/client/angular4/src/app/login/login.component.html new file mode 100644 index 0000000000..d87b91a7bb --- /dev/null +++ b/spring-security-anguar/client/angular4/src/app/login/login.component.html @@ -0,0 +1,18 @@ +
+

Login

+
+
+ + +
Username is required
+
+
+ + +
Password is required
+
+
+ +
+
+
diff --git a/spring-security-anguar/client/angular4/src/app/login/login.component.ts b/spring-security-anguar/client/angular4/src/app/login/login.component.ts new file mode 100644 index 0000000000..2a2dc102af --- /dev/null +++ b/spring-security-anguar/client/angular4/src/app/login/login.component.ts @@ -0,0 +1,39 @@ +import { Component, OnInit } from '@angular/core'; +import { Router, ActivatedRoute } from '@angular/router'; +import { Http } from '@angular/http'; + +@Component({ + selector: 'login', + templateUrl: './login.component.html' +}) + +export class LoginComponent implements OnInit { + model: any = {}; + + constructor( + private route: ActivatedRoute, + private router: Router, + private http: Http) { } + + ngOnInit() { + sessionStorage.setItem('token', ''); + } + + + login() { + let url = 'http://localhost:8082/login'; + let result = this.http.post(url, { + userName: this.model.username, + password: this.model.password + }). + map(res => res.json()). + subscribe(isValid => { + if (isValid) { + sessionStorage.setItem('token', btoa(this.model.username + ':' + this.model.password)); + this.router.navigate(['']); + } else { + alert("Authentication failed.") + } + }); + } +} diff --git a/spring-security-anguar/client/angular4/src/index.html b/spring-security-anguar/client/angular4/src/index.html new file mode 100644 index 0000000000..c716820396 --- /dev/null +++ b/spring-security-anguar/client/angular4/src/index.html @@ -0,0 +1,14 @@ + + + + + AngularCRUD + + + + + + + + + \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/main.ts b/spring-security-anguar/client/angular4/src/main.ts new file mode 100644 index 0000000000..49db98ae89 --- /dev/null +++ b/spring-security-anguar/client/angular4/src/main.ts @@ -0,0 +1,5 @@ +import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; + +import { AppModule } from './app/app.module'; + +platformBrowserDynamic().bootstrapModule(AppModule); \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/polyfills.ts b/spring-security-anguar/client/angular4/src/polyfills.ts new file mode 100644 index 0000000000..a4984ced57 --- /dev/null +++ b/spring-security-anguar/client/angular4/src/polyfills.ts @@ -0,0 +1,68 @@ +/** + * This file includes polyfills needed by Angular and is loaded before the app. + * You can add your own extra polyfills to this file. + * + * This file is divided into 2 sections: + * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. + * 2. Application imports. Files imported after ZoneJS that should be loaded before your main + * file. + * + * The current setup is for so-called "evergreen" browsers; the last versions of browsers that + * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), + * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. + * + * Learn more in https://angular.io/docs/ts/latest/guide/browser-support.html + */ + +/*************************************************************************************************** + * BROWSER POLYFILLS + */ + +/** IE9, IE10 and IE11 requires all of the following polyfills. **/ +// import 'core-js/es6/symbol'; +// import 'core-js/es6/object'; +// import 'core-js/es6/function'; +// import 'core-js/es6/parse-int'; +// import 'core-js/es6/parse-float'; +// import 'core-js/es6/number'; +// import 'core-js/es6/math'; +// import 'core-js/es6/string'; +// import 'core-js/es6/date'; +// import 'core-js/es6/array'; +// import 'core-js/es6/regexp'; +// import 'core-js/es6/map'; +// import 'core-js/es6/weak-map'; +// import 'core-js/es6/set'; +/** IE10 and IE11 requires the following for NgClass support on SVG elements */ +// import 'classlist.js'; // Run `npm install --save classlist.js`. +/** Evergreen browsers require these. **/ +import 'core-js/es6/reflect'; +import 'core-js/es7/reflect'; + + +/** + * Required to support Web Animations `@angular/animation`. + * Needed for: All but Chrome, Firefox and Opera. http://caniuse.com/#feat=web-animation + **/ +// import 'web-animations-js'; // Run `npm install --save web-animations-js`. + + +/*************************************************************************************************** + * Zone JS is required by Angular itself. + */ +import 'zone.js/dist/zone'; // Included with Angular CLI. + + +/*************************************************************************************************** + * APPLICATION IMPORTS + */ + +/** + * Date, currency, decimal and percent pipes. + * Needed for: All but Chrome, Firefox, Edge, IE11 and Safari 10 + */ +// import 'intl'; // Run `npm install --save intl`. +/** + * Need to import at least one locale-data with intl. + */ +// import 'intl/locale-data/jsonp/en'; \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/styles.css b/spring-security-anguar/client/angular4/src/styles.css new file mode 100644 index 0000000000..cdd2d591d8 --- /dev/null +++ b/spring-security-anguar/client/angular4/src/styles.css @@ -0,0 +1,7 @@ +a { + cursor: pointer; +} + +.help-block { + font-size: 12px; +} \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/src/tsconfig.app.json b/spring-security-anguar/client/angular4/src/tsconfig.app.json new file mode 100644 index 0000000000..213ce42a1b --- /dev/null +++ b/spring-security-anguar/client/angular4/src/tsconfig.app.json @@ -0,0 +1,13 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "outDir": "../out-tsc/app", + "baseUrl": "./", + "module": "es2015", + "types": [] + }, + "exclude": [ + "test.ts", + "**/*.spec.ts" + ] +} \ No newline at end of file diff --git a/spring-security-anguar/client/angular4/tsconfig.json b/spring-security-anguar/client/angular4/tsconfig.json new file mode 100644 index 0000000000..ef44e2862b --- /dev/null +++ b/spring-security-anguar/client/angular4/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compileOnSave": false, + "compilerOptions": { + "baseUrl": "./", + "outDir": "./dist/out-tsc", + "sourceMap": true, + "declaration": false, + "moduleResolution": "node", + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "target": "es5", + "typeRoots": [ + "node_modules/@types" + ], + "lib": [ + "es2017", + "dom" + ] + } +} diff --git a/spring-security-anguar/client/angular4/tslint.json b/spring-security-anguar/client/angular4/tslint.json new file mode 100644 index 0000000000..3ea984c776 --- /dev/null +++ b/spring-security-anguar/client/angular4/tslint.json @@ -0,0 +1,130 @@ +{ + "rulesDirectory": [ + "node_modules/codelyzer" + ], + "rules": { + "arrow-return-shorthand": true, + "callable-types": true, + "class-name": true, + "comment-format": [ + true, + "check-space" + ], + "curly": true, + "deprecation": { + "severity": "warn" + }, + "eofline": true, + "forin": true, + "import-blacklist": [ + true, + "rxjs/Rx" + ], + "import-spacing": true, + "indent": [ + true, + "spaces" + ], + "interface-over-type-literal": true, + "label-position": true, + "max-line-length": [ + true, + 140 + ], + "member-access": false, + "member-ordering": [ + true, + { + "order": [ + "static-field", + "instance-field", + "static-method", + "instance-method" + ] + } + ], + "no-arg": true, + "no-bitwise": true, + "no-console": [ + true, + "debug", + "info", + "time", + "timeEnd", + "trace" + ], + "no-construct": true, + "no-debugger": true, + "no-duplicate-super": true, + "no-empty": false, + "no-empty-interface": true, + "no-eval": true, + "no-inferrable-types": [ + true, + "ignore-params" + ], + "no-misused-new": true, + "no-non-null-assertion": true, + "no-shadowed-variable": true, + "no-string-literal": false, + "no-string-throw": true, + "no-switch-case-fall-through": true, + "no-trailing-whitespace": true, + "no-unnecessary-initializer": true, + "no-unused-expression": true, + "no-use-before-declare": true, + "no-var-keyword": true, + "object-literal-sort-keys": false, + "one-line": [ + true, + "check-open-brace", + "check-catch", + "check-else", + "check-whitespace" + ], + "prefer-const": true, + "quotemark": [ + true, + "single" + ], + "radix": true, + "semicolon": [ + true, + "always" + ], + "triple-equals": [ + true, + "allow-null-check" + ], + "typedef-whitespace": [ + true, + { + "call-signature": "nospace", + "index-signature": "nospace", + "parameter": "nospace", + "property-declaration": "nospace", + "variable-declaration": "nospace" + } + ], + "unified-signatures": true, + "variable-name": false, + "whitespace": [ + true, + "check-branch", + "check-decl", + "check-operator", + "check-separator", + "check-type" + ], + "no-output-on-prefix": true, + "use-input-property-decorator": true, + "use-output-property-decorator": true, + "use-host-property-decorator": true, + "no-input-rename": true, + "no-output-rename": true, + "use-life-cycle-interface": true, + "use-pipe-transform-interface": true, + "component-class-suffix": true, + "directive-class-suffix": true + } +} diff --git a/spring-security-anguar/client/angular5/.angular-cli.json b/spring-security-anguar/client/angular5/.angular-cli.json new file mode 100644 index 0000000000..d390652214 --- /dev/null +++ b/spring-security-anguar/client/angular5/.angular-cli.json @@ -0,0 +1,61 @@ +{ + "$schema": "./node_modules/@angular/cli/lib/config/schema.json", + "project": { + "name": "angular5" + }, + "apps": [ + { + "root": "src", + "outDir": "dist", + "assets": [ + "assets", + "favicon.ico" + ], + "index": "index.html", + "main": "main.ts", + "polyfills": "polyfills.ts", + "test": "test.ts", + "tsconfig": "tsconfig.app.json", + "testTsconfig": "tsconfig.spec.json", + "prefix": "app", + "styles": [ + "styles.css", + "../node_modules/ngx-toastr/toastr.css" + ], + "scripts": [], + "environmentSource": "environments/environment.ts", + "environments": { + "dev": "environments/environment.ts", + "prod": "environments/environment.prod.ts" + } + } + ], + "e2e": { + "protractor": { + "config": "./protractor.conf.js" + } + }, + "lint": [ + { + "project": "src/tsconfig.app.json", + "exclude": "**/node_modules/**" + }, + { + "project": "src/tsconfig.spec.json", + "exclude": "**/node_modules/**" + }, + { + "project": "e2e/tsconfig.e2e.json", + "exclude": "**/node_modules/**" + } + ], + "test": { + "karma": { + "config": "./karma.conf.js" + } + }, + "defaults": { + "styleExt": "css", + "component": {} + } +} \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/package.json b/spring-security-anguar/client/angular5/package.json new file mode 100644 index 0000000000..45f455d191 --- /dev/null +++ b/spring-security-anguar/client/angular5/package.json @@ -0,0 +1,50 @@ +{ + "name": "angular5", + "version": "0.0.0", + "license": "MIT", + "scripts": { + "ng": "ng", + "start": "ng serve", + "build": "ng build --prod", + "test": "ng test", + "lint": "ng lint", + "e2e": "ng e2e" + }, + "private": true, + "dependencies": { + "@angular/animations": "^5.0.0", + "@angular/common": "^5.0.0", + "@angular/compiler": "^5.0.0", + "@angular/core": "^5.0.0", + "@angular/forms": "^5.0.0", + "@angular/http": "^5.0.0", + "@angular/platform-browser": "^5.0.0", + "@angular/platform-browser-dynamic": "^5.0.0", + "@angular/router": "^5.0.0", + "core-js": "^2.4.1", + "ngx-toastr": "^8.1.1", + "rxjs": "^5.5.2", + "zone.js": "^0.8.14" + }, + "devDependencies": { + "@angular/cli": "^1.6.6", + "@angular/compiler-cli": "^5.0.0", + "@angular/language-service": "^5.0.0", + "@types/jasmine": "~2.5.53", + "@types/jasminewd2": "~2.0.2", + "@types/node": "~6.0.60", + "codelyzer": "^4.0.1", + "jasmine-core": "~2.6.2", + "jasmine-spec-reporter": "~4.1.0", + "karma": "~1.7.0", + "karma-chrome-launcher": "~2.1.1", + "karma-cli": "~1.0.1", + "karma-coverage-istanbul-reporter": "^1.2.1", + "karma-jasmine": "~1.1.0", + "karma-jasmine-html-reporter": "^0.2.2", + "protractor": "~5.1.2", + "ts-node": "~3.2.0", + "tslint": "~5.7.0", + "typescript": "~2.4.2" + } +} diff --git a/spring-security-anguar/client/angular5/src/app/app.component.html b/spring-security-anguar/client/angular5/src/app/app.component.html new file mode 100644 index 0000000000..7f77adea7b --- /dev/null +++ b/spring-security-anguar/client/angular5/src/app/app.component.html @@ -0,0 +1,7 @@ +
+
+
+ +
+
+
diff --git a/spring-security-anguar/client/angular5/src/app/app.component.ts b/spring-security-anguar/client/angular5/src/app/app.component.ts new file mode 100644 index 0000000000..36f986c63f --- /dev/null +++ b/spring-security-anguar/client/angular5/src/app/app.component.ts @@ -0,0 +1,8 @@ +import { Component } from '@angular/core'; + +@Component({ + selector: 'app-root', + templateUrl: './app.component.html' +}) + +export class AppComponent { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/app/app.module.ts b/spring-security-anguar/client/angular5/src/app/app.module.ts new file mode 100644 index 0000000000..9e840e6f13 --- /dev/null +++ b/spring-security-anguar/client/angular5/src/app/app.module.ts @@ -0,0 +1,29 @@ +import { NgModule } from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; +import { FormsModule } from '@angular/forms'; +import { HttpModule } from '@angular/http'; + +// used to create fake backend + +import { AppComponent } from './app.component'; +import { routing } from './app.routing'; + +import { HomeComponent } from './home/home.component'; +import { LoginComponent } from './login/login.component'; + +@NgModule({ + imports: [ + BrowserModule, + FormsModule, + HttpModule, + routing + ], + declarations: [ + AppComponent, + HomeComponent, + LoginComponent + ], + bootstrap: [AppComponent] +}) + +export class AppModule { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/app/app.routing.ts b/spring-security-anguar/client/angular5/src/app/app.routing.ts new file mode 100644 index 0000000000..c794fc5c50 --- /dev/null +++ b/spring-security-anguar/client/angular5/src/app/app.routing.ts @@ -0,0 +1,12 @@ +import { Routes, RouterModule } from '@angular/router'; + +import { HomeComponent } from './home/home.component'; +import { LoginComponent } from './login/login.component'; + +const appRoutes: Routes = [ + { path: '', component: HomeComponent}, + { path: 'login', component: LoginComponent }, + { path: '**', redirectTo: '' } +]; + +export const routing = RouterModule.forRoot(appRoutes); \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/app/home/home.component.html b/spring-security-anguar/client/angular5/src/app/home/home.component.html new file mode 100644 index 0000000000..7ccd2c2a3a --- /dev/null +++ b/spring-security-anguar/client/angular5/src/app/home/home.component.html @@ -0,0 +1,4 @@ +
+

Hi {{userName}}!

+

Logout

+
\ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/app/home/home.component.ts b/spring-security-anguar/client/angular5/src/app/home/home.component.ts new file mode 100644 index 0000000000..7fe1ac4eff --- /dev/null +++ b/spring-security-anguar/client/angular5/src/app/home/home.component.ts @@ -0,0 +1,37 @@ +import { Component, OnInit } from '@angular/core'; +import { Http, Headers, RequestOptions } from '@angular/http'; +import 'rxjs/add/operator/map' + +@Component({ + selector:'home', + templateUrl: './home.component.html' +}) + +export class HomeComponent implements OnInit { + + userName: string; + + constructor(private http: Http) { } + + ngOnInit() { + let url = 'http://localhost:8082/user'; + + let headers:Headers = new Headers({ + 'Authorization': 'Basic ' + sessionStorage.getItem('token') + }) + let options = new RequestOptions({headers: headers}); + this.http.post(url,{}, options).map( + res => res.json(), + error => { + if(error.status == 401) + alert('Unauthorized'); + } + ).subscribe(principal => { + this.userName = principal.name; + }); + } + + logout() { + sessionStorage.setItem('token', ''); + } +} \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/app/login/login.component.html b/spring-security-anguar/client/angular5/src/app/login/login.component.html new file mode 100644 index 0000000000..d87b91a7bb --- /dev/null +++ b/spring-security-anguar/client/angular5/src/app/login/login.component.html @@ -0,0 +1,18 @@ +
+

Login

+
+
+ + +
Username is required
+
+
+ + +
Password is required
+
+
+ +
+
+
diff --git a/spring-security-anguar/client/angular5/src/app/login/login.component.ts b/spring-security-anguar/client/angular5/src/app/login/login.component.ts new file mode 100644 index 0000000000..2db8f32871 --- /dev/null +++ b/spring-security-anguar/client/angular5/src/app/login/login.component.ts @@ -0,0 +1,37 @@ +import { Component, OnInit } from '@angular/core'; +import { Router, ActivatedRoute } from '@angular/router'; +import { Http } from '@angular/http'; + +@Component({ + selector: 'login', + templateUrl: './login.component.html' +}) + +export class LoginComponent implements OnInit { + model: any = {}; + + constructor( + private route: ActivatedRoute, + private router: Router, + private http: Http) { } + + ngOnInit() { + sessionStorage.setItem('token', ''); + } + + + login() { + let url = 'http://localhost:8082/login'; + let result = this.http.post(url, { + userName: this.model.username, + password: this.model.password + }).map(res => res.json()).subscribe(isValid => { + if (isValid) { + sessionStorage.setItem('token', btoa(this.model.username + ':' + this.model.password)); + this.router.navigate(['']); + } else { + alert("Authentication failed.") + } + }); + } +} diff --git a/spring-security-anguar/client/angular5/src/index.html b/spring-security-anguar/client/angular5/src/index.html new file mode 100644 index 0000000000..c716820396 --- /dev/null +++ b/spring-security-anguar/client/angular5/src/index.html @@ -0,0 +1,14 @@ + + + + + AngularCRUD + + + + + + + + + \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/main.ts b/spring-security-anguar/client/angular5/src/main.ts new file mode 100644 index 0000000000..49db98ae89 --- /dev/null +++ b/spring-security-anguar/client/angular5/src/main.ts @@ -0,0 +1,5 @@ +import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; + +import { AppModule } from './app/app.module'; + +platformBrowserDynamic().bootstrapModule(AppModule); \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/polyfills.ts b/spring-security-anguar/client/angular5/src/polyfills.ts new file mode 100644 index 0000000000..a4984ced57 --- /dev/null +++ b/spring-security-anguar/client/angular5/src/polyfills.ts @@ -0,0 +1,68 @@ +/** + * This file includes polyfills needed by Angular and is loaded before the app. + * You can add your own extra polyfills to this file. + * + * This file is divided into 2 sections: + * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. + * 2. Application imports. Files imported after ZoneJS that should be loaded before your main + * file. + * + * The current setup is for so-called "evergreen" browsers; the last versions of browsers that + * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), + * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. + * + * Learn more in https://angular.io/docs/ts/latest/guide/browser-support.html + */ + +/*************************************************************************************************** + * BROWSER POLYFILLS + */ + +/** IE9, IE10 and IE11 requires all of the following polyfills. **/ +// import 'core-js/es6/symbol'; +// import 'core-js/es6/object'; +// import 'core-js/es6/function'; +// import 'core-js/es6/parse-int'; +// import 'core-js/es6/parse-float'; +// import 'core-js/es6/number'; +// import 'core-js/es6/math'; +// import 'core-js/es6/string'; +// import 'core-js/es6/date'; +// import 'core-js/es6/array'; +// import 'core-js/es6/regexp'; +// import 'core-js/es6/map'; +// import 'core-js/es6/weak-map'; +// import 'core-js/es6/set'; +/** IE10 and IE11 requires the following for NgClass support on SVG elements */ +// import 'classlist.js'; // Run `npm install --save classlist.js`. +/** Evergreen browsers require these. **/ +import 'core-js/es6/reflect'; +import 'core-js/es7/reflect'; + + +/** + * Required to support Web Animations `@angular/animation`. + * Needed for: All but Chrome, Firefox and Opera. http://caniuse.com/#feat=web-animation + **/ +// import 'web-animations-js'; // Run `npm install --save web-animations-js`. + + +/*************************************************************************************************** + * Zone JS is required by Angular itself. + */ +import 'zone.js/dist/zone'; // Included with Angular CLI. + + +/*************************************************************************************************** + * APPLICATION IMPORTS + */ + +/** + * Date, currency, decimal and percent pipes. + * Needed for: All but Chrome, Firefox, Edge, IE11 and Safari 10 + */ +// import 'intl'; // Run `npm install --save intl`. +/** + * Need to import at least one locale-data with intl. + */ +// import 'intl/locale-data/jsonp/en'; \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/styles.css b/spring-security-anguar/client/angular5/src/styles.css new file mode 100644 index 0000000000..cdd2d591d8 --- /dev/null +++ b/spring-security-anguar/client/angular5/src/styles.css @@ -0,0 +1,7 @@ +a { + cursor: pointer; +} + +.help-block { + font-size: 12px; +} \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/src/tsconfig.app.json b/spring-security-anguar/client/angular5/src/tsconfig.app.json new file mode 100644 index 0000000000..213ce42a1b --- /dev/null +++ b/spring-security-anguar/client/angular5/src/tsconfig.app.json @@ -0,0 +1,13 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "outDir": "../out-tsc/app", + "baseUrl": "./", + "module": "es2015", + "types": [] + }, + "exclude": [ + "test.ts", + "**/*.spec.ts" + ] +} \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/tsconfig.json b/spring-security-anguar/client/angular5/tsconfig.json new file mode 100644 index 0000000000..0fdb5c817d --- /dev/null +++ b/spring-security-anguar/client/angular5/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compileOnSave": false, + "compilerOptions": { + "outDir": "./dist/out-tsc", + "sourceMap": true, + "declaration": false, + "moduleResolution": "node", + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "target": "es5", + "typeRoots": [ + "node_modules/@types" + ], + "lib": [ + "es2017", + "dom" + ] + } +} \ No newline at end of file diff --git a/spring-security-anguar/client/angular5/tslint.json b/spring-security-anguar/client/angular5/tslint.json new file mode 100644 index 0000000000..1c1d53b0d9 --- /dev/null +++ b/spring-security-anguar/client/angular5/tslint.json @@ -0,0 +1,144 @@ +{ + "rulesDirectory": [ + "node_modules/codelyzer" + ], + "rules": { + "arrow-return-shorthand": true, + "callable-types": true, + "class-name": true, + "comment-format": [ + true, + "check-space" + ], + "curly": true, + "deprecation": { + "severity": "warn" + }, + "eofline": true, + "forin": true, + "import-blacklist": [ + true, + "rxjs", + "rxjs/Rx" + ], + "import-spacing": true, + "indent": [ + true, + "spaces" + ], + "interface-over-type-literal": true, + "label-position": true, + "max-line-length": [ + true, + 140 + ], + "member-access": false, + "member-ordering": [ + true, + { + "order": [ + "static-field", + "instance-field", + "static-method", + "instance-method" + ] + } + ], + "no-arg": true, + "no-bitwise": true, + "no-console": [ + true, + "debug", + "info", + "time", + "timeEnd", + "trace" + ], + "no-construct": true, + "no-debugger": true, + "no-duplicate-super": true, + "no-empty": false, + "no-empty-interface": true, + "no-eval": true, + "no-inferrable-types": [ + true, + "ignore-params" + ], + "no-misused-new": true, + "no-non-null-assertion": true, + "no-shadowed-variable": true, + "no-string-literal": false, + "no-string-throw": true, + "no-switch-case-fall-through": true, + "no-trailing-whitespace": true, + "no-unnecessary-initializer": true, + "no-unused-expression": true, + "no-use-before-declare": true, + "no-var-keyword": true, + "object-literal-sort-keys": false, + "one-line": [ + true, + "check-open-brace", + "check-catch", + "check-else", + "check-whitespace" + ], + "prefer-const": true, + "quotemark": [ + true, + "single" + ], + "radix": true, + "semicolon": [ + true, + "always" + ], + "triple-equals": [ + true, + "allow-null-check" + ], + "typedef-whitespace": [ + true, + { + "call-signature": "nospace", + "index-signature": "nospace", + "parameter": "nospace", + "property-declaration": "nospace", + "variable-declaration": "nospace" + } + ], + "typeof-compare": true, + "unified-signatures": true, + "variable-name": false, + "whitespace": [ + true, + "check-branch", + "check-decl", + "check-operator", + "check-separator", + "check-type" + ], + "directive-selector": [ + true, + "attribute", + "app", + "camelCase" + ], + "component-selector": [ + true, + "element", + "app", + "kebab-case" + ], + "no-output-on-prefix": true, + "use-input-property-decorator": true, + "use-output-property-decorator": true, + "use-host-property-decorator": true, + "no-input-rename": true, + "no-output-rename": true, + "use-life-cycle-interface": true, + "use-pipe-transform-interface": true, + "component-class-suffix": true, + "directive-class-suffix": true + } +} \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/angular.json b/spring-security-anguar/client/angular6/angular.json new file mode 100644 index 0000000000..0168c58817 --- /dev/null +++ b/spring-security-anguar/client/angular6/angular.json @@ -0,0 +1,127 @@ +{ + "$schema": "./node_modules/@angular/cli/lib/config/schema.json", + "version": 1, + "newProjectRoot": "projects", + "projects": { + "angular6-login": { + "root": "", + "sourceRoot": "src", + "projectType": "application", + "prefix": "app", + "schematics": {}, + "architect": { + "build": { + "builder": "@angular-devkit/build-angular:browser", + "options": { + "outputPath": "dist/angular6-login", + "index": "src/index.html", + "main": "src/main.ts", + "polyfills": "src/polyfills.ts", + "tsConfig": "src/tsconfig.app.json", + "assets": [ + "src/favicon.ico", + "src/assets" + ], + "styles": [ + "src/styles.css" + ], + "scripts": [] + }, + "configurations": { + "production": { + "fileReplacements": [ + { + "replace": "src/environments/environment.ts", + "with": "src/environments/environment.prod.ts" + } + ], + "optimization": true, + "outputHashing": "all", + "sourceMap": false, + "extractCss": true, + "namedChunks": false, + "aot": true, + "extractLicenses": true, + "vendorChunk": false, + "buildOptimizer": true + } + } + }, + "serve": { + "builder": "@angular-devkit/build-angular:dev-server", + "options": { + "browserTarget": "angular6-login:build" + }, + "configurations": { + "production": { + "browserTarget": "angular6-login:build:production" + } + } + }, + "extract-i18n": { + "builder": "@angular-devkit/build-angular:extract-i18n", + "options": { + "browserTarget": "angular6-login:build" + } + }, + "test": { + "builder": "@angular-devkit/build-angular:karma", + "options": { + "main": "src/test.ts", + "polyfills": "src/polyfills.ts", + "tsConfig": "src/tsconfig.spec.json", + "karmaConfig": "src/karma.conf.js", + "styles": [ + "src/styles.css" + ], + "scripts": [], + "assets": [ + "src/favicon.ico", + "src/assets" + ] + } + }, + "lint": { + "builder": "@angular-devkit/build-angular:tslint", + "options": { + "tsConfig": [ + "src/tsconfig.app.json", + "src/tsconfig.spec.json" + ], + "exclude": [ + "**/node_modules/**" + ] + } + } + } + }, + "angular6-login-e2e": { + "root": "e2e/", + "projectType": "application", + "architect": { + "e2e": { + "builder": "@angular-devkit/build-angular:protractor", + "options": { + "protractorConfig": "e2e/protractor.conf.js", + "devServerTarget": "angular6-login:serve" + }, + "configurations": { + "production": { + "devServerTarget": "angular6-login:serve:production" + } + } + }, + "lint": { + "builder": "@angular-devkit/build-angular:tslint", + "options": { + "tsConfig": "e2e/tsconfig.e2e.json", + "exclude": [ + "**/node_modules/**" + ] + } + } + } + } + }, + "defaultProject": "angular6-login" +} \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/package.json b/spring-security-anguar/client/angular6/package.json new file mode 100644 index 0000000000..a0adcd03f3 --- /dev/null +++ b/spring-security-anguar/client/angular6/package.json @@ -0,0 +1,48 @@ +{ + "name": "angular6-login", + "version": "0.0.0", + "scripts": { + "ng": "ng", + "start": "ng serve", + "build": "ng build", + "test": "ng test", + "lint": "ng lint", + "e2e": "ng e2e" + }, + "private": true, + "dependencies": { + "@angular/animations": "^6.0.3", + "@angular/common": "^6.0.3", + "@angular/compiler": "^6.0.3", + "@angular/core": "^6.0.3", + "@angular/forms": "^6.0.3", + "@angular/http": "^6.0.3", + "@angular/platform-browser": "^6.0.3", + "@angular/platform-browser-dynamic": "^6.0.3", + "@angular/router": "^6.0.3", + "core-js": "^2.5.4", + "rxjs": "^6.0.0", + "zone.js": "^0.8.26" + }, + "devDependencies": { + "@angular/compiler-cli": "^6.0.3", + "@angular-devkit/build-angular": "~0.6.8", + "typescript": "~2.7.2", + "@angular/cli": "~6.0.8", + "@angular/language-service": "^6.0.3", + "@types/jasmine": "~2.8.6", + "@types/jasminewd2": "~2.0.3", + "@types/node": "~8.9.4", + "codelyzer": "~4.2.1", + "jasmine-core": "~2.99.1", + "jasmine-spec-reporter": "~4.2.1", + "karma": "~1.7.1", + "karma-chrome-launcher": "~2.2.0", + "karma-coverage-istanbul-reporter": "~2.0.0", + "karma-jasmine": "~1.1.1", + "karma-jasmine-html-reporter": "^0.2.2", + "protractor": "~5.3.0", + "ts-node": "~5.0.1", + "tslint": "~5.9.1" + } +} diff --git a/spring-security-anguar/client/angular6/src/app/app.component.html b/spring-security-anguar/client/angular6/src/app/app.component.html new file mode 100644 index 0000000000..6bf00f3018 --- /dev/null +++ b/spring-security-anguar/client/angular6/src/app/app.component.html @@ -0,0 +1,2 @@ + + diff --git a/spring-security-anguar/client/angular6/src/app/app.component.ts b/spring-security-anguar/client/angular6/src/app/app.component.ts new file mode 100644 index 0000000000..36f986c63f --- /dev/null +++ b/spring-security-anguar/client/angular6/src/app/app.component.ts @@ -0,0 +1,8 @@ +import { Component } from '@angular/core'; + +@Component({ + selector: 'app-root', + templateUrl: './app.component.html' +}) + +export class AppComponent { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/app/app.module.ts b/spring-security-anguar/client/angular6/src/app/app.module.ts new file mode 100644 index 0000000000..4f9a792e19 --- /dev/null +++ b/spring-security-anguar/client/angular6/src/app/app.module.ts @@ -0,0 +1,27 @@ +import { NgModule } from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; +import { FormsModule } from '@angular/forms'; +import { HttpClientModule } from '@angular/common/http'; + +import { AppComponent } from './app.component'; +import { routing } from './app.routing'; + +import { HomeComponent } from './home/home.component'; +import { LoginComponent } from './login/login.component'; + +@NgModule({ + imports: [ + BrowserModule, + FormsModule, + HttpClientModule, + routing + ], + declarations: [ + AppComponent, + HomeComponent, + LoginComponent + ], + bootstrap: [AppComponent] +}) + +export class AppModule { } \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/app/app.routing.ts b/spring-security-anguar/client/angular6/src/app/app.routing.ts new file mode 100644 index 0000000000..e5f5e04aa8 --- /dev/null +++ b/spring-security-anguar/client/angular6/src/app/app.routing.ts @@ -0,0 +1,11 @@ +import { Routes, RouterModule } from '@angular/router'; +import { HomeComponent } from './home/home.component'; +import { LoginComponent } from './login/login.component'; + +const appRoutes: Routes = [ + { path: '', component: HomeComponent }, + { path: 'login', component: LoginComponent }, + { path: '**', redirectTo: '' } +]; + +export const routing = RouterModule.forRoot(appRoutes); \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/app/home/home.component.html b/spring-security-anguar/client/angular6/src/app/home/home.component.html new file mode 100644 index 0000000000..7ccd2c2a3a --- /dev/null +++ b/spring-security-anguar/client/angular6/src/app/home/home.component.html @@ -0,0 +1,4 @@ +
+

Hi {{userName}}!

+

Logout

+
\ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/app/home/home.component.ts b/spring-security-anguar/client/angular6/src/app/home/home.component.ts new file mode 100644 index 0000000000..6c9fcfd97f --- /dev/null +++ b/spring-security-anguar/client/angular6/src/app/home/home.component.ts @@ -0,0 +1,49 @@ +import { Component, OnInit } from '@angular/core'; +import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http'; +import { Observable, throwError } from 'rxjs'; +import { catchError, map, tap} from 'rxjs/operators'; +@Component({ + selector: 'home', + templateUrl: './home.component.html' +}) + +export class HomeComponent implements OnInit { + + userName: string; + + constructor(private http: HttpClient) { } + + ngOnInit() { + let url = 'http://localhost:8082/user'; + + let headers: HttpHeaders = new HttpHeaders({ + 'Authorization': 'Basic ' + sessionStorage.getItem('token') + }); + + let options = { headers: headers }; + this.http.post>(url, {}, options). + subscribe(principal => { + this.userName = principal['name']; + }, + error => { + if(error.status == 401) + alert('Unauthorized'); + } + ); + } + + logout() { + sessionStorage.setItem('token', ''); + } + private handleError(error: HttpErrorResponse) { + if (error.error instanceof ErrorEvent) { + console.error('An error occurred:', error.error.message); + } else { + console.error( + `Backend returned code ${error.status}, ` + + `body was: ${error.error}`); + } + return throwError( + 'Something bad happened; please try again later.'); + }; +} \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/app/login/login.component.html b/spring-security-anguar/client/angular6/src/app/login/login.component.html new file mode 100644 index 0000000000..4291206469 --- /dev/null +++ b/spring-security-anguar/client/angular6/src/app/login/login.component.html @@ -0,0 +1,15 @@ +
+
+ + +
Username is required
+
+
+ + +
Password is required
+
+
+ +
+
\ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/app/login/login.component.ts b/spring-security-anguar/client/angular6/src/app/login/login.component.ts new file mode 100644 index 0000000000..27af9ebba5 --- /dev/null +++ b/spring-security-anguar/client/angular6/src/app/login/login.component.ts @@ -0,0 +1,39 @@ +import { Component, OnInit } from '@angular/core'; +import { Router, ActivatedRoute } from '@angular/router'; +import { HttpClient } from '@angular/common/http'; +import { Observable } from 'rxjs'; + +@Component({ + selector: 'login', + templateUrl: './login.component.html' +}) + +export class LoginComponent implements OnInit { + + model: any = {}; + + constructor( + private route: ActivatedRoute, + private router: Router, + private http: HttpClient + ) { } + + ngOnInit() { + sessionStorage.setItem('token', ''); + } + + login() { + let url = 'http://localhost:8082/login'; + this.http.post>(url, { + userName: this.model.username, + password: this.model.password + }).subscribe(isValid => { + if (isValid) { + sessionStorage.setItem('token', btoa(this.model.username + ':' + this.model.password)); + this.router.navigate(['']); + } else { + alert("Authentication failed.") + } + }); + } +} diff --git a/spring-security-anguar/client/angular6/src/index.html b/spring-security-anguar/client/angular6/src/index.html new file mode 100644 index 0000000000..c716820396 --- /dev/null +++ b/spring-security-anguar/client/angular6/src/index.html @@ -0,0 +1,14 @@ + + + + + AngularCRUD + + + + + + + + + \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/main.ts b/spring-security-anguar/client/angular6/src/main.ts new file mode 100644 index 0000000000..49db98ae89 --- /dev/null +++ b/spring-security-anguar/client/angular6/src/main.ts @@ -0,0 +1,5 @@ +import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; + +import { AppModule } from './app/app.module'; + +platformBrowserDynamic().bootstrapModule(AppModule); \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/polyfills.ts b/spring-security-anguar/client/angular6/src/polyfills.ts new file mode 100644 index 0000000000..a4984ced57 --- /dev/null +++ b/spring-security-anguar/client/angular6/src/polyfills.ts @@ -0,0 +1,68 @@ +/** + * This file includes polyfills needed by Angular and is loaded before the app. + * You can add your own extra polyfills to this file. + * + * This file is divided into 2 sections: + * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. + * 2. Application imports. Files imported after ZoneJS that should be loaded before your main + * file. + * + * The current setup is for so-called "evergreen" browsers; the last versions of browsers that + * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), + * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. + * + * Learn more in https://angular.io/docs/ts/latest/guide/browser-support.html + */ + +/*************************************************************************************************** + * BROWSER POLYFILLS + */ + +/** IE9, IE10 and IE11 requires all of the following polyfills. **/ +// import 'core-js/es6/symbol'; +// import 'core-js/es6/object'; +// import 'core-js/es6/function'; +// import 'core-js/es6/parse-int'; +// import 'core-js/es6/parse-float'; +// import 'core-js/es6/number'; +// import 'core-js/es6/math'; +// import 'core-js/es6/string'; +// import 'core-js/es6/date'; +// import 'core-js/es6/array'; +// import 'core-js/es6/regexp'; +// import 'core-js/es6/map'; +// import 'core-js/es6/weak-map'; +// import 'core-js/es6/set'; +/** IE10 and IE11 requires the following for NgClass support on SVG elements */ +// import 'classlist.js'; // Run `npm install --save classlist.js`. +/** Evergreen browsers require these. **/ +import 'core-js/es6/reflect'; +import 'core-js/es7/reflect'; + + +/** + * Required to support Web Animations `@angular/animation`. + * Needed for: All but Chrome, Firefox and Opera. http://caniuse.com/#feat=web-animation + **/ +// import 'web-animations-js'; // Run `npm install --save web-animations-js`. + + +/*************************************************************************************************** + * Zone JS is required by Angular itself. + */ +import 'zone.js/dist/zone'; // Included with Angular CLI. + + +/*************************************************************************************************** + * APPLICATION IMPORTS + */ + +/** + * Date, currency, decimal and percent pipes. + * Needed for: All but Chrome, Firefox, Edge, IE11 and Safari 10 + */ +// import 'intl'; // Run `npm install --save intl`. +/** + * Need to import at least one locale-data with intl. + */ +// import 'intl/locale-data/jsonp/en'; \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/styles.css b/spring-security-anguar/client/angular6/src/styles.css new file mode 100644 index 0000000000..cdd2d591d8 --- /dev/null +++ b/spring-security-anguar/client/angular6/src/styles.css @@ -0,0 +1,7 @@ +a { + cursor: pointer; +} + +.help-block { + font-size: 12px; +} \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/src/tsconfig.app.json b/spring-security-anguar/client/angular6/src/tsconfig.app.json new file mode 100644 index 0000000000..213ce42a1b --- /dev/null +++ b/spring-security-anguar/client/angular6/src/tsconfig.app.json @@ -0,0 +1,13 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "outDir": "../out-tsc/app", + "baseUrl": "./", + "module": "es2015", + "types": [] + }, + "exclude": [ + "test.ts", + "**/*.spec.ts" + ] +} \ No newline at end of file diff --git a/spring-security-anguar/client/angular6/tsconfig.json b/spring-security-anguar/client/angular6/tsconfig.json new file mode 100644 index 0000000000..ef44e2862b --- /dev/null +++ b/spring-security-anguar/client/angular6/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compileOnSave": false, + "compilerOptions": { + "baseUrl": "./", + "outDir": "./dist/out-tsc", + "sourceMap": true, + "declaration": false, + "moduleResolution": "node", + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "target": "es5", + "typeRoots": [ + "node_modules/@types" + ], + "lib": [ + "es2017", + "dom" + ] + } +} diff --git a/spring-security-anguar/client/angular6/tslint.json b/spring-security-anguar/client/angular6/tslint.json new file mode 100644 index 0000000000..3ea984c776 --- /dev/null +++ b/spring-security-anguar/client/angular6/tslint.json @@ -0,0 +1,130 @@ +{ + "rulesDirectory": [ + "node_modules/codelyzer" + ], + "rules": { + "arrow-return-shorthand": true, + "callable-types": true, + "class-name": true, + "comment-format": [ + true, + "check-space" + ], + "curly": true, + "deprecation": { + "severity": "warn" + }, + "eofline": true, + "forin": true, + "import-blacklist": [ + true, + "rxjs/Rx" + ], + "import-spacing": true, + "indent": [ + true, + "spaces" + ], + "interface-over-type-literal": true, + "label-position": true, + "max-line-length": [ + true, + 140 + ], + "member-access": false, + "member-ordering": [ + true, + { + "order": [ + "static-field", + "instance-field", + "static-method", + "instance-method" + ] + } + ], + "no-arg": true, + "no-bitwise": true, + "no-console": [ + true, + "debug", + "info", + "time", + "timeEnd", + "trace" + ], + "no-construct": true, + "no-debugger": true, + "no-duplicate-super": true, + "no-empty": false, + "no-empty-interface": true, + "no-eval": true, + "no-inferrable-types": [ + true, + "ignore-params" + ], + "no-misused-new": true, + "no-non-null-assertion": true, + "no-shadowed-variable": true, + "no-string-literal": false, + "no-string-throw": true, + "no-switch-case-fall-through": true, + "no-trailing-whitespace": true, + "no-unnecessary-initializer": true, + "no-unused-expression": true, + "no-use-before-declare": true, + "no-var-keyword": true, + "object-literal-sort-keys": false, + "one-line": [ + true, + "check-open-brace", + "check-catch", + "check-else", + "check-whitespace" + ], + "prefer-const": true, + "quotemark": [ + true, + "single" + ], + "radix": true, + "semicolon": [ + true, + "always" + ], + "triple-equals": [ + true, + "allow-null-check" + ], + "typedef-whitespace": [ + true, + { + "call-signature": "nospace", + "index-signature": "nospace", + "parameter": "nospace", + "property-declaration": "nospace", + "variable-declaration": "nospace" + } + ], + "unified-signatures": true, + "variable-name": false, + "whitespace": [ + true, + "check-branch", + "check-decl", + "check-operator", + "check-separator", + "check-type" + ], + "no-output-on-prefix": true, + "use-input-property-decorator": true, + "use-output-property-decorator": true, + "use-host-property-decorator": true, + "no-input-rename": true, + "no-output-rename": true, + "use-life-cycle-interface": true, + "use-pipe-transform-interface": true, + "component-class-suffix": true, + "directive-class-suffix": true + } +} diff --git a/spring-security-anguar/server/pom.xml b/spring-security-anguar/server/pom.xml new file mode 100644 index 0000000000..39de129c87 --- /dev/null +++ b/spring-security-anguar/server/pom.xml @@ -0,0 +1,71 @@ + + + 4.0.0 + com.baeldung + spring-security-angular + 0.0.1-SNAPSHOT + jar + spring-boot-security-rest + Spring Boot Security REST + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + + + + + + org.springframework.boot + spring-boot-dependencies + 1.5.9.RELEASE + pom + import + + + + + + + + + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.security + spring-security-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + UTF-8 + UTF-8 + + + diff --git a/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/basicauth/SpringBootSecurityApplication.java b/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/basicauth/SpringBootSecurityApplication.java new file mode 100644 index 0000000000..681c7590a8 --- /dev/null +++ b/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/basicauth/SpringBootSecurityApplication.java @@ -0,0 +1,14 @@ +package com.baeldung.springbootsecurityrest.basicauth; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication(scanBasePackages = "com.baeldung.springbootsecurityrest") +@EnableAutoConfiguration +public class SpringBootSecurityApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootSecurityApplication.class, args); + } +} diff --git a/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/basicauth/config/BasicAuthConfiguration.java b/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/basicauth/config/BasicAuthConfiguration.java new file mode 100644 index 0000000000..3ed301439c --- /dev/null +++ b/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/basicauth/config/BasicAuthConfiguration.java @@ -0,0 +1,34 @@ +package com.baeldung.springbootsecurityrest.basicauth.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpMethod; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +@Configuration +@EnableWebSecurity +public class BasicAuthConfiguration extends WebSecurityConfigurerAdapter { + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth + .inMemoryAuthentication() + .withUser("user") + .password("password") + .roles("USER"); + } + + @Override + protected void configure(HttpSecurity http) throws Exception { + http.csrf().disable() + .authorizeRequests() + .antMatchers(HttpMethod.OPTIONS, "/**").permitAll() + .antMatchers("/login").permitAll() + .anyRequest() + .authenticated() + .and() + .httpBasic(); + } +} diff --git a/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/controller/UserController.java b/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/controller/UserController.java new file mode 100644 index 0000000000..825290ff2d --- /dev/null +++ b/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/controller/UserController.java @@ -0,0 +1,32 @@ +package com.baeldung.springbootsecurityrest.controller; + +import java.security.Principal; +import java.util.Base64; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.springbootsecurityrest.vo.User; + +@RestController +@CrossOrigin +public class UserController { + + @RequestMapping("/login") + public boolean login(@RequestBody User user) { + if(user.getUserName().equals("user") && user.getPassword().equals("password")) { + return true; + } + return false; + } + + @RequestMapping("/user") + public Principal user(HttpServletRequest request) { + String authToken = request.getHeader("Authorization").substring("Basic".length()).trim(); + return () -> new String(Base64.getDecoder().decode(authToken)).split(":")[0]; + } +} diff --git a/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/vo/User.java b/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/vo/User.java new file mode 100644 index 0000000000..0eda5ce9ec --- /dev/null +++ b/spring-security-anguar/server/src/main/java/com/baeldung/springbootsecurityrest/vo/User.java @@ -0,0 +1,21 @@ +package com.baeldung.springbootsecurityrest.vo; + + +public class User { + + private String userName; + private String password; + + public String getUserName() { + return userName; + } + public void setUserName(String userName) { + this.userName = userName; + } + public String getPassword() { + return password; + } + public void setPassword(String password) { + this.password = password; + } +} diff --git a/spring-security-anguar/server/src/main/resources/application.properties b/spring-security-anguar/server/src/main/resources/application.properties new file mode 100644 index 0000000000..565d97a7b0 --- /dev/null +++ b/spring-security-anguar/server/src/main/resources/application.properties @@ -0,0 +1,5 @@ +#spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration +#security.user.password=password +#security.oauth2.client.client-id=client +#security.oauth2.client.client-secret=secret +server.port=8082 diff --git a/spring-security-anguar/server/src/test/java/com/baeldung/springbootsecurityrest/BasicAuthConfigurationIntegrationTest.java b/spring-security-anguar/server/src/test/java/com/baeldung/springbootsecurityrest/BasicAuthConfigurationIntegrationTest.java new file mode 100644 index 0000000000..952a0806a1 --- /dev/null +++ b/spring-security-anguar/server/src/test/java/com/baeldung/springbootsecurityrest/BasicAuthConfigurationIntegrationTest.java @@ -0,0 +1,87 @@ +package com.baeldung.springbootsecurityrest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.springbootsecurityrest.basicauth.SpringBootSecurityApplication; +import com.baeldung.springbootsecurityrest.vo.User; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = RANDOM_PORT, classes = SpringBootSecurityApplication.class) +public class BasicAuthConfigurationIntegrationTest { + + TestRestTemplate restTemplate; + URL base; + + @LocalServerPort int port; + + @Before + public void setUp() throws MalformedURLException { + restTemplate = new TestRestTemplate("user", "password"); + base = new URL("http://localhost:" + port); + } + + @Test + public void givenCorrectCredentials_whenLogin_ThenSuccess() throws IllegalStateException, IOException { + restTemplate = new TestRestTemplate(); + User user = new User(); + user.setUserName("user"); + user.setPassword("password"); + ResponseEntity response = restTemplate.postForEntity(base.toString()+"/login",user, String.class); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertTrue(response + .getBody() + .contains("true")); + } + + @Test + public void givenWrongCredentials_whenLogin_ThenReturnFalse() throws IllegalStateException, IOException { + restTemplate = new TestRestTemplate(); + User user = new User(); + user.setUserName("user"); + user.setPassword("wrongpassword"); + ResponseEntity response = restTemplate.postForEntity(base.toString()+"/login",user, String.class); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertTrue(response + .getBody() + .contains("false")); + } + + @Test + public void givenLoggedInUser_whenRequestsHomePage_ThenSuccess() throws IllegalStateException, IOException { + ResponseEntity response = restTemplate.getForEntity(base.toString()+"/user", String.class); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertTrue(response + .getBody() + .contains("user")); + } + + @Test + public void givenWrongCredentials_whenRequestsHomePage_ThenUnauthorized() throws IllegalStateException, IOException { + restTemplate = new TestRestTemplate("user", "wrongpassword"); + ResponseEntity response = restTemplate.getForEntity(base.toString()+"/user", String.class); + + assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); + assertTrue(response + .getBody() + .contains("Unauthorized")); + } +}