Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
V
VIPSLogic
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
VIPS
VIPSLogic
Commits
656e1fea
Commit
656e1fea
authored
9 months ago
by
Tor-Einar Skog
Browse files
Options
Downloads
Patches
Plain Diff
Autoformatted
parent
bcca20a4
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/no/nibio/vips/logic/authenticate/AuthenticationFilter.java
+49
-56
49 additions, 56 deletions
...o/nibio/vips/logic/authenticate/AuthenticationFilter.java
with
49 additions
and
56 deletions
src/main/java/no/nibio/vips/logic/authenticate/AuthenticationFilter.java
+
49
−
56
View file @
656e1fea
/*
/*
* Copyright (c) 2022 NIBIO <http://www.nibio.no/>.
* Copyright (c) 2022 NIBIO <http://www.nibio.no/>.
*
*
* This program is free software: you can redistribute it and/or modify
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General
* it under the terms of the GNU Affero General Public License as published by
* Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* the Free Software Foundation, either version 3 of the License, or
* later version.
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* details.
* GNU Affero General Public License for more details.
*
*
* You should have received a copy of the GNU Affero General Public License
* You should have received a copy of the GNU Affero General Public License
along with this program. If not, see
*
along with this program. If not, see
<https://www.gnu.org/licenses/>.
* <https://www.gnu.org/licenses/>.
*
*
*/
*/
...
@@ -23,7 +21,12 @@ import java.io.IOException;
...
@@ -23,7 +21,12 @@ import java.io.IOException;
import
java.net.URLEncoder
;
import
java.net.URLEncoder
;
import
java.util.UUID
;
import
java.util.UUID
;
import
javax.ejb.EJB
;
import
javax.ejb.EJB
;
import
javax.servlet.*
;
import
javax.servlet.Filter
;
import
javax.servlet.FilterChain
;
import
javax.servlet.FilterConfig
;
import
javax.servlet.ServletException
;
import
javax.servlet.ServletRequest
;
import
javax.servlet.ServletResponse
;
import
javax.servlet.http.Cookie
;
import
javax.servlet.http.Cookie
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
javax.servlet.http.HttpServletResponse
;
...
@@ -34,79 +37,71 @@ import no.nibio.vips.util.ServletUtil;
...
@@ -34,79 +37,71 @@ import no.nibio.vips.util.ServletUtil;
/**
/**
* Ensures that user accessing a restricted resource is actually logged in. Redirects to login page if not
* Ensures that user accessing a restricted resource is actually logged in. Redirects to login page if not
*
* @copyright 2013-2022 <a href="http://www.nibio.no">NIBIO</a>
* @copyright 2013-2022 <a href="http://www.nibio.no">NIBIO</a>
* @author Tor-Einar Skog <tor-einar.skog@nibio.no>
* @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/
*/
public
class
AuthenticationFilter
implements
Filter
{
public
class
AuthenticationFilter
implements
Filter
{
@EJB
@EJB
UserBean
userBean
;
UserBean
userBean
;
// The URLs that do not require login
// The URLs that do not require login
private
String
[]
unprivilegedURLs
;
private
String
[]
unprivilegedURLs
;
@Override
@Override
public
void
init
(
FilterConfig
filterConfig
)
throws
ServletException
{
public
void
init
(
FilterConfig
filterConfig
)
throws
ServletException
{
this
.
setUnprivilegedURLs
(
Globals
.
UNPRIVILEGED_URLS
);
this
.
setUnprivilegedURLs
(
Globals
.
UNPRIVILEGED_URLS
);
}
}
@Override
@Override
public
void
doFilter
(
ServletRequest
request
,
ServletResponse
response
,
FilterChain
chain
)
throws
IOException
,
ServletException
{
public
void
doFilter
(
ServletRequest
request
,
ServletResponse
response
,
FilterChain
chain
)
throws
IOException
,
ServletException
{
HttpServletRequest
httpRequest
=
(
HttpServletRequest
)
request
;
HttpServletRequest
httpRequest
=
(
HttpServletRequest
)
request
;
/*
/*
// For debugging
* // For debugging BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
* String line; while((line = reader.readLine()) != null) { System.out.println(line); }
String line;
*/
while((line = reader.readLine()) != null)
if
(
isUnprivilegedURL
(
httpRequest
))
{
{
System.out.println(line);
}*/
if
(
isUnprivilegedURL
(
httpRequest
))
{
chain
.
doFilter
(
request
,
response
);
chain
.
doFilter
(
request
,
response
);
//return
;
System
.
out
.
println
(
"Uprivileged URL: "
+
httpRequest
.
getRequestURI
())
;
}
// return;
else
}
else
{
{
System
.
out
.
println
(
"Privileged URL: "
+
httpRequest
.
getRequestURI
());
// First: Check for session variable
// First: Check for session variable
boolean
clientAuthenticated
=
(
httpRequest
.
getSession
().
getAttribute
(
"user"
)
!=
null
&&
httpRequest
.
getSession
().
getAttribute
(
"user"
)
instanceof
VipsLogicUser
);
boolean
clientAuthenticated
=
(
httpRequest
.
getSession
().
getAttribute
(
"user"
)
!=
null
&&
httpRequest
.
getSession
().
getAttribute
(
"user"
)
instanceof
VipsLogicUser
);
// Then for UUID cookie that has not expired
// Then for UUID cookie that has not expired
boolean
clientRemembered
=
false
;
boolean
clientRemembered
=
false
;
Cookie
remembered
=
ServletUtil
.
getCookie
(
httpRequest
,
"rememberedUser"
);
Cookie
remembered
=
ServletUtil
.
getCookie
(
httpRequest
,
"rememberedUser"
);
if
(
remembered
!=
null
)
if
(
remembered
!=
null
)
{
{
VipsLogicUser
user
=
userBean
.
findVipsLogicUser
(
UUID
.
fromString
(
remembered
.
getValue
()));
VipsLogicUser
user
=
userBean
.
findVipsLogicUser
(
UUID
.
fromString
(
remembered
.
getValue
()));
if
(
user
!=
null
)
if
(
user
!=
null
)
{
{
httpRequest
.
getSession
().
setAttribute
(
"user"
,
user
);
httpRequest
.
getSession
().
setAttribute
(
"user"
,
user
);
clientRemembered
=
true
;
clientRemembered
=
true
;
}
}
}
}
if
(!
clientAuthenticated
&&
!
clientRemembered
)
if
(!
clientAuthenticated
&&
!
clientRemembered
)
{
{
String
nextPageDirective
=
""
;
String
nextPageDirective
=
""
;
if
(!
httpRequest
.
getServletPath
().
equals
(
"/login"
))
if
(!
httpRequest
.
getServletPath
().
equals
(
"/login"
))
{
{
String
nextPage
=
ServletUtil
.
getFullRequestURI
(
httpRequest
);
String
nextPage
=
ServletUtil
.
getFullRequestURI
(
httpRequest
);
nextPageDirective
=
"?nextPage="
+
URLEncoder
.
encode
(
nextPage
,
"UTF-8"
);
nextPageDirective
=
"?nextPage="
+
URLEncoder
.
encode
(
nextPage
,
"UTF-8"
);
}
}
((
HttpServletResponse
)
response
).
sendRedirect
(
Globals
.
PROTOCOL
+
"://"
+
ServletUtil
.
getServerName
(
httpRequest
)
+
"/login"
+
nextPageDirective
);
((
HttpServletResponse
)
response
).
sendRedirect
(
Globals
.
PROTOCOL
+
"://"
}
+
ServletUtil
.
getServerName
(
httpRequest
)
+
"/login"
+
nextPageDirective
);
else
}
else
{
{
chain
.
doFilter
(
request
,
response
);
chain
.
doFilter
(
request
,
response
);
}
}
//return;
//
return;
}
}
}
}
private
boolean
isUnprivilegedURL
(
HttpServletRequest
request
)
{
private
boolean
isUnprivilegedURL
(
HttpServletRequest
request
)
{
String
path
=
request
.
getServletPath
();
String
path
=
request
.
getServletPath
();
for
(
String
unprivilegedURL
:
this
.
getUnprivilegedURLs
())
for
(
String
unprivilegedURL
:
this
.
getUnprivilegedURLs
())
{
{
if
(
path
.
contains
(
unprivilegedURL
))
{
if
(
path
.
contains
(
unprivilegedURL
))
{
return
true
;
return
true
;
}
}
}
}
...
@@ -115,12 +110,10 @@ public class AuthenticationFilter implements Filter{
...
@@ -115,12 +110,10 @@ public class AuthenticationFilter implements Filter{
@Override
@Override
public
void
destroy
()
{
public
void
destroy
()
{
}
}
/**
/**
* @return the upriviligerteURLer
* @return the upriviligerteURLer
...
@@ -136,6 +129,6 @@ public class AuthenticationFilter implements Filter{
...
@@ -136,6 +129,6 @@ public class AuthenticationFilter implements Filter{
this
.
unprivilegedURLs
=
unprivilegedURLs
;
this
.
unprivilegedURLs
=
unprivilegedURLs
;
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment