Keycloak integration with ReactJS

Integration of the keycloak with react JS

 

Pre-requisites: Node module and keycloak installed on the machine.

Note :  Make sure both don’t share the default port number 8080

 

Step 1 : Go to your react project folder and install node module packages by

npm install  

Step 2 : Install keycloak packages from npm

npm install keycloak-js—save –dev

Step 3  : add the below code to your main js file

Import the keycloak js to the js file

import Keycloak from “keycloak-js”;

const kc = Keycloak(‘/keycloak.json’);

kc.init({onLoad: ‘check-sso’}).success(authenticated => {

if (authenticated) {

store.getState().keycloak = kc;

 

setInterval(() => {

kc.updateToken(10).error(() => kc.logout());

}, 10000);

 

ReactDOM.render(app, document.getElementById(“app”));

 

 

} else {

// show possibly other page here…

 

 

kc.login();

}

});

axios.interceptors.request.use(config => {

config.headers = {

‘Content-Type’: ‘application/json’,

Accept: ‘application/json’,

Authorization: ‘Bearer ‘ + kc.token

 

};

return config;

});

 

Step 4: install the latest keycloak and configure the keycloak

Keep the keyclok.json file in the js file directory of react js

Step 5: Verify the tocken which is append by keycloak redirect url .

 

Step 6 : run node module

Npm start

…………………………………………………………………………………………………………………………………

Configure the Keycloak realm ::

Create New realm : Demo

Create new client  : demo

 

 

 

 

 

Configure the urls :: Root URL http://localhost:8691/index.html

* Valid Redirect URIs :: http://localhost:8691/*

Note : this url end must have * else it won’t redirect .

Base URL :: http://localhost:8691/index.html

Web Origins :: http://localhost:8691

 

  1. Add the User Roles to the clients
  2. Go to the installation and select keycloak OIDC json and get the JSOn , it look like

{

“realm”: “demo”,

“auth-server-url”: “http://localhost:8080/auth”,

“ssl-required”: “external”,

“resource”: “demo”,

“public-client”: true,

“use-resource-role-mappings”: true,

“disable-trust-manager”: true,

“allow-any-hostname”: true

}

  1. Copy to your project js or root folder with extension .json
  2. Add the users and assign the same role as clients

    you are all set to run the  react keycloak integration

 

Use of Axios :

Axios Component for React with child function callback. This is intended to allow in render async requests.

Features

  • Component driven
  • Child function callback (error, response, isLoading) => { }
  • Auto cancel previous requests
  • Debounce to prevent rapid calls.
  • Request only invoked on prop change and isReady state.
  • Callback props for onSuccessonError, and onLoading
  • Supports custom axios instances through props or a <AxiosProvider … >

Installing

Using npm:

$ npm install react-axios

Also install the required peer dependancies if you have not already done so:

$ npm install axios

$ npm install react

$ npm install prop-types

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Advertisements

Java Restful Web Services

Slect button or select tag with hyper link or link to another page

<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>JSP Page</title>
</head>
<body>
<form name=”link”>
<select name=”mylink” OnChange=”location.href=link.mylink.options[selectedIndex].value”>
<option selected>Please Select…
<option value=”ex.com”>Clients
<option value=”ex1.com”>Projects
<option value=”ex2.com”>Consultants
<option value=”ex3.com”>Timecard
</select>
</form>
</body>
</html>

YACC programs ( VTU syllabus)

1.Program to test the validity of a simple expression involving operators +,-,* and /.

Lex Specification

…………………………………..

%{

#include “y.tab.h”

%}

%%

[_a-zA-Z][_a-zA-Z0-9]* return ID;

[0-9]+
return NUM;
. return yytext[0];

\n return 0;

%%

………………………………….

Yacc Specification

%{

#include

%}

%token NUM ID

%left ‘+’ ‘-‘

%left ‘*’ ‘/’

%nonassoc UMINUS

%%

exp: exp ‘+’ exp

| exp ‘-‘ exp

| exp ‘/’ exp

| exp ‘*’ exp

| ‘-‘ exp %prec UMINUS

| ‘(‘ exp ‘)’

| NUM

| ID

;

%%

int main()

{

printf(“\n Enter an expression:”);

yyparse();

printf(“\n valid expression”);

return 0;

}

void yyerror(){

printf(“\n Invalid expression”);

exit(0);

}

Output:

…………………………………………..

lex l1.l

yacc -d y1.y

cc lex.yy.c y.tab.c -ll

./a.out

Enter an expression:a+(b-c)/a

valid expression

………………………………………………………………………………………………………….

2.Program to recognise the nested IF control statements and display the number of levels of nesting.

Lex Specification

WS [ \t]*

%{

#include “y.tab.h”

%}

%%

{WS}”if”{WS} {return IF;}

[-+_A-Za-z0-9!=><]+ {return STMT;}

. {return yytext[0];}

\n {return 0;}

%%

…………………………….

Yacc specification

%{

#include

int nlevel=0;

%}

%token IF STMT

%%

start:ifs{printf(“\n valid statement\n”);}

ifs:IF cond st{nlevel++;}

;

st:simpst

|”{“compst”}”

|ifs

;

simpst:STMT”;”

|”;”

;

compst:simpst compst

|ifs compst

|simpst

|ifs

;

cond:”(“STMT”)”

;

%%

int main()

{ printf(“\n enter an expression\n”);

yyparse();

printf(“\n number of levels of nesting=%d\n”,nlevel);

return 0;

}

void yyerror()

{ printf(“\n invalid”);

exit(0);

}

…………………………………..

Output:

lex lfile2.l

yacc -d yfile2.y

cc lex.yy.c y.tab.c -ll

./a.out

enter an expression

if(a==b)a=0;

valid statement

………………………………………………………………………………………………………………..

3.Program to recognize valid arithmatic expression that uses operators+,-,*and /.

Lex Specification

…………………………….

%{

#include “y.tab.h”

%}

%%

([0-9]+|[0-9]*\.[0-9]+) return NUM;
. return yytext[0];

\n return 0;

%%

Yacc Specification

…………………..

%{

#include

%}

%token NUM

%left ‘+’ ‘-‘

%left ‘*’ ‘/’

%nonassoc UMINUS

%%

exp : exp ‘+’ exp

| exp ‘-‘ exp

| exp ‘/’ exp

| ‘-‘ exp %prec UMINUS

| exp ‘*’ exp

| ‘(‘ exp ‘)’

| NUM

;

%%

int main()

{

printf(“\n Enter an arithmatic expression”);

yyparse();

printf(“\n Valid expression”);

return 0;

}

void yyerror(){

printf(“\n Invalid expression”);

return 0;

}

void yyerror(){

printf(“\n Invalid expression”);

exit(0);

}

………………………………….

Output

lex l3.l

yacc -d y3.y

cc lex.yy.c y.tab.c -ll

./a.out

Enter an arithmatic expression1+3*4

Valid expression

………………………………………………………………………………….

4. Program to recognize a valid variable.

Lex Specification

……………………….

%{

#include “y.tab.h”

%}

%%

[a-z][-a-zA-Z0-9]* {return VAR;}

. return yytext[0];

\n return 0;

%%

Yacc Specification

…………………………….

%{

#include

%}

%token VAR

%%

start:VAR

|

;

%%

int main()

{ printf(“\n enter a variable\n”);

yyparse();

printf(“\n valid variable”);

return 0;

}

void yyerror()

{ printf(“\n invalid variable”);

exit(0);

}

Output:

lex lfile4.l

yacc -d yfile4.y

cc lex.yy.c y.tab.c -ll

./a.out

Enter a variable

cn12

valid variable

……………………………………………

5.Evaluate the arithmatic expression +,-,*,/

Lex Specification

…………………………..

%{

#include “y.tab.h”

%}

%%

([0-9]+|[0-9]*\.[0-9]+)+ {yylval.fval=atof(yytext);return NUM;}

. return yytext[0];

\n return 0;

%%

Yacc Specification

…………………………….

%{

#include

%}

%union {float fval;}

%token NUM

%type e

%type start

%left’+”-‘

%left’*”/’

%nonassoc UMINUS

%%

start:e{printf(“=%2.2f”,$$);}

;

e:e’+’e{$$=$1+$3;}

|e’-‘e{$$=$1-$3;}

|e’*’e{$$=$1*$3;}

|e’/’e{if($3==0)

yyerror(“diveded by zero”);

else

$$=$1/$3;

}

|’-‘e %prec UMINUS{$$=-$2;}

|'(‘e’)'{$$=$2;}

|NUM{$$=$1;}

;

%%

int main()

{

printf(“\n enter aritmetic expression\n”);

yyparse();

printf(“\n valid expression\n”);

return 0;

}

void yyerror()

{ printf(“\n invalid expression\n”);

exit(0);

}

…………………………………

Output:

lex lfile5.l

yacc -d yfile.y

cc lex.yy.c y.tab.c -ll

./a.out

Enter aritmetic expression

1+5*4

=21.00

valid expression

……………………………………………………………………………………..

6. Program to recognize the string ‘abbb’,’ab’,’a’ using grammer (a^nb^n,n>=0).

Lex Specification

……………………

%{

#include “y.tab.h”

%}

%%

a return A;

b return B;

. return yytext[0];

\n return 0;

%%

yacc Specification

………………………..

%{

#include

%}

%token A B

%%

start:sa sb

|sa

|sb

;

sa: A sa

| A

;

sb: B sb

|B

;

%%

int main()

{

printf(“\n enter a string of A’s followed by B\n”);

printf(“for the following grammer a^nb^n,n,m>=0\n”);

yyparse();

printf(“valid input\n”);

return 0;

}

void yyerror()

{ printf(“\n invalid input\n”);

exit(0);

}

Output:

……………………….

lex lfile6.l

yacc -d yfile6.y

cc lex.yy.c y.tab.c -ll

./a.out

enter a string of A’s followed by B

for the following grammer a^nb^n,n,m>=0

aabbb

valid input

…………………………………………………………………………………..

7. Program to recognize the grammer(a^b,n>=10).

Lex Specification

…………………………….

%{

#include “y.tab.h”

%}

%%

a return A;

b return B;

. return yytext[0];

\n return 0;

%%

Yacc Specification

…………………

%{

#include

%}

%token A B

%%

start:tena next

;

next: A next

| B

;

tena:A A A A A A A A A A

;

%%

int main()

{

printf(“\n enter a string of A’s followed by B\n”);

printf(“for the following grammer a^nb,n>=10\n”);

yyparse();

printf(“valid input\n”);

return 0;

}

void yyerror()

{ printf(“\n invalid input\n”);

exit(0);

}

Output:

………………

lex lfile7.l

yacc -d yfile7.y

cc lex.yy.c y.tab.c -ll

./a.out

enter a string of A’s followed by B

for the following grammer a^nb,n>=10

aaaaaaaaaaaab

valid input

How to remove/disable windows security alert from the system/pc

There is a simpler method to remove the security center.

* As admin, Start –> Run ; type “services.msc” and press enter. This brings up a list of Windows services that you can start, stop and configure.
* Find the Security center, double click on it and change the Security Center from Auto start to Manual. Select OK and close the services dialog box.
* Restart. Security Center won’t run at all.

Simple Random alphanumeric value generator – java

Java its very easy to generate the alphanumeric values. This coding helps to randomly generate the alphanumeric values , according to your request. you can easily generate how many character you want by passing the value.

import java.util.Random;

public class AlphaNumericValueGenerator {

public String randGenerator(int range){
// you can simply pass the range bellow also, range is how many charecter you want. If only uppercase letter you want just delete the lowercase letters.
//int range =5;
String randString = “”;
if (range <= 0){
return randString;
}
StringBuffer sb = new StringBuffer();
String block = “abcdefghijklmnopqrstuvwxyz1234567890ABCDEFIJKLMNOPQRSTUVWXYZ”;
sb.append(block).append(block.toUpperCase()).append(“0123456789”);
block = sb.toString();
sb = new StringBuffer();
Random random = new Random();
try {
for (int i = 0; i < range; i++) {
sb.append(Character.toString(block.charAt(random.nextInt(block.length() – 1))));
}
randString = sb.toString();
} catch (ArrayIndexOutOfBoundsException e) {

} catch (NumberFormatException e) {

} catch (Exception e) {

}
return randString;
// return xx;

}

}

code:By collection

How can I upload/insert csv/text files in mysql database

Here is the solution for uploading the csv or text file to mysql database from query.
LOAD DATA INFILE ‘C:/Documents and Settings/user/Desktop/mycsvfile.csv’ INTO TABLE mytable(field_name);

My csv file contains the data of type varchar, id is auto increment type so no problem while inserting this data.

My data is like-

GYXKF5KX
H2VPL3W2
7MS4AJ5L
HVBN6RKB
UU7MV8CB
PYUTV4PW
WYNSTKKG
VPATS88K
R56Q6XAS
HQF46BM6
Y97NVM84
7UHK4AR3
LWUG9AAX

Its automatically inserting all of this fields one by one in row.
If its a txt file just change the extension in the filed.
If you have more fields of data you can check the conditions in file.

LOAD DATA LOCAL INFILE ‘C:/Documents and Settings/user/Desktop/mycsvfile.csv”
INTO TABLE mytable
FIELDS TERMINATED BY ‘,’
LINES TERMINATED BY ‘\n’
(field1, filed2, field3);

or also you can check

LOAD DATA LOCAL INFILE ‘C:/Documents and Settings/user/Desktop/mycsvfile.csv’ INTO TABLE ‘mytable’ FIELDS TERMINATED BY ‘,’ ENCLOSED BY ‘”‘ ESCAPED BY ‘\\’ LINES TERMINATED BY ‘\r\n’ IGNORE 1 lines”);

This is depending upon your file.

Thanks Guru